#include <bits/stdc++.h>

using namespace std;

// thank you geothermal :goat:
void __deb(const array<long long, 2>& t) {cerr << '[' << t[0] << ':' << t[1] << ']';}
void __deb(const vector<long long>& t) {cerr << '[';
     for(long long i : t)
         cerr << i << ' ';

    cerr << ']';}
void __deb(int t) {cerr << t;}
void __deb(long t) {cerr << t;}
void __deb(long long t) {cerr << t;}
void __deb(unsigned int t) {cerr << t;}
void __deb(unsigned long t) {cerr << t;}
void __deb(unsigned long long t) {cerr << t;}
void __deb(float t) {cerr << t;}
void __deb(double t) {cerr << t;}
void __deb(long double t) {cerr << t;}
void __deb(char t) {cerr << '\'' << t << '\'';}
void __deb(const char *t) {cerr << '\"' << t << '\"';}
void __deb(const string &t) {cerr << '\"' << t << '\"';}
void __deb(bool t) {cerr << (t ? "true" : "false");}
template<typename T, typename V>
void __deb(const pair<T, V> &x){ cerr << '{'; __deb(x.first); cerr << ", "; __deb(x.second); cerr << '}'; }
template<typename T>
void __deb(const T &x){
    int f = 0; cerr << '{'; 
    for(auto &i: x){ if(f++) cerr << ", "; __deb(i);}
    cerr << "}";
}
void _rdeb(vector<string> &names, int ind){}
template<typename T, typename... V>
void _rdeb(vector<string> &names, int ind, T t, V... v){
    if(ind != 0) cerr << "==============\n";
    cerr << names[ind] << '\n'; __deb(t); cerr << '\n';
    _rdeb(names, ind+1, v...);
}
template<typename... V>
void _deb(string vars, V... v){
    vector<string> names(1); int in{};
    for(char c : vars)
        if(c == ',' && in == 0){
            while(names.back().size() && isspace(names.back().back())) names.back().pop_back();
            names.push_back("");
        }else{
            if(c == '(') in++; if(c == ')') in--;
            if(!isspace(c) || names.back().size()) names.back().push_back(c);
        }
    while(names.back().size() && isspace(names.back().back())) names.back().pop_back();
    _rdeb(names, 0, v...);
}
#ifdef DEBUG
#define deb(x...) {                              \
    cerr << __func__ << ':' << __LINE__ << '\n'; \
    _deb(#x, x);                                 \
    cerr << '\n';                                \
}
#define debsep(x...) {                           \
    cerr << __func__ << ':' << __LINE__ << '\n'; \
    _deb(#x, x);                                 \
    cerr << "****************************\n";    \
}
#else
#define deb(x...)
#define debsep(x...)
#endif

#define ar array
#define ll long long
typedef int uci;
#define int long long
#define F first
#define S second
typedef complex<double> cd;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define irep(i, a, b) for(int i = a; i > (b); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
#define ve vector
typedef ve<int> vi;
typedef ve<vi> vvi;

seed_seq seq{
    (uint64_t) chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count(),
    (uint64_t) __builtin_ia32_rdtsc(),
    (uint64_t) (uintptr_t) make_unique<char>().get()
};
mt19937 rng(seq);
mt19937_64 lrng(seq);

struct debugger{
    template <typename T>
    debugger& operator<<(T &a){
        #ifdef DEBUG
            cerr << a;
        #endif
        return *this;
    }
    template <typename T>
    debugger& operator<<(T &&a){
        #ifdef DEBUG
            cerr << a;
        #endif
        return *this;
    }
} deb;

const double PI = acos(-1.0);
const int MAX_N = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;

//! function insert

//THINK FIRST, CODE SECOND
//DON'T GET STUCK ON ONE STRATEGY
//CALM DOWNNN FOR ONCE IN YOUR LIFE
//REDUCE
//COUGH E??!?!?!! O.O
//uwu i see you ryan

int exp(int n, int p){
    int out{1};
    while(p){
        if(p&1){
            out *= n;
            out %= MOD;
        }
        p >>= 1;
        n *= n;
        n %= MOD;
    }
    return out;
}

void solve() {
    string s, t;
    cin >> s >> t;
    int n = sz(s), m = sz(t);

    map<ar<int, 2>, vi> inds;
    ve<ar<int, 2>> ind_hash(n);
    ar<int, 2> hash{};
    int p = uniform_real_distribution<double>(53, MOD-2)(lrng);
    int p2 = uniform_real_distribution<double>(53, MOD-2)(lrng);
    for(int i = n-1; i >= 0; --i){
        if(i+m < n){
            hash[0] -= (exp(p, m-1)*(s[i+m]-'a'+1))%MOD;
            hash[0] %= MOD;
            if(hash[0] < 0)
                hash[0] += MOD;

            hash[1] -= (exp(p2, m-1)*(s[i+m]-'a'+1))%MOD;
            hash[1] %= MOD;
            if(hash[1] < 0)
                hash[1] += MOD;
        }
        hash[0] *= p;
        hash[0] %= MOD;
        hash[1] *= p2;
        hash[1] %= MOD;

        hash[0] += (s[i]-'a'+1);
        hash[0] %= MOD;
        hash[1] += (s[i]-'a'+1);
        hash[1] %= MOD;

        if(i+m <= n)
            inds[hash].push_back(i);

        ind_hash[i] = hash;
    }


    for(auto &t : inds)
        reverse(all(t.S));


    ve<ar<int, 26>> pre(n+1), post(n+1);

    for(int i{}; i < n; ++i){
        for(int j{}; j < 26; ++j)
            pre[i+1][j] = pre[i][j];
        pre[i+1][s[i]-'a']++;
    }
    for(int i = n-1; i >= 0; --i){
        for(int j{}; j < 26; ++j)
            post[i][j] = post[i+1][j];
        post[i][s[i]-'a']++;
    }


    ar<int, 26> count{};
    int per{};
    for(char c : t){
        int tot{1};
        for(int j{}; j < c-'a'; ++j){
            tot += count[j];
        }
        per += tot * (c-'a'+1);
        count[c-'a']++;
    }

    vi precost(n);
    {
        ar<int, 26> pc{};
        for(int i{}; i < n; ++i){
            int tot{1};
            for(int j{}; j < s[i]-'a'; ++j){
                tot += pc[j];
            }
            precost[i] = (i == 0 ? 0 : precost[i-1]) + (tot * (s[i]-'a'+1));
            pc[s[i]-'a']++;
        }
    }


    auto getcounts = [&](int l, int r){
        if(l > r)
            return ar<int, 26>{};
        ar<int, 26> ret = pre[r+1];
        for(int i{}; i < 26; ++i)
            ret[i] -= pre[l][i];
        return ret;
    };

    vi dp(n, LINF);
    ve<ar<int, 26>> changes(n);

    auto getcountschanged = [&](int l){
        ar<int, 26> ret = getcounts(l, n-1);

        for(int i{}; i < 26; ++i){
            ret[i] += changes[l][i];
        }

        return ret;
    };

    vi repc(n);
    {
        ar<int, 26> run{};
        int crun{};
        for(int i{}; i < n; ++i){
            if(i-m >= 0){
                for(int j = s[i-m]-'a'+1; j < 26; ++j){
                    crun -= (j+1)*run[j];
                }
                crun -= s[i-m]-'a'+1;
                run[s[i-m]-'a']--;
            }

            for(int j{}; j < s[i]-'a'; ++j){
                crun += (s[i]-'a'+1)*run[j];
            }
            run[s[i]-'a']++;
            crun += s[i]-'a'+1;

            if(i-m+1 >= 0)
                repc[i-m+1] = crun;
        }
    }


    for(int i = n-1; i >= 0; --i){
        if(i+m > n)
            continue;

        auto &occ = inds[ind_hash[i]];
        auto next_occ = lower_bound(all(occ), i+m);

        if(next_occ == occ.end()){
            // first one

            dp[i] = precost.back();
            ar<int, 26> replacing = getcounts(i, i+m-1);
            ar<int, 26> after = getcounts(i+m, n-1);
            ar<int, 26> before = getcounts(0, i-1);


            int accumcount{};
            int accumreplacing{};
            int accumbefore{};
            for(int k{}; k < 26; ++k){
                dp[i] -= accumreplacing * (k+1) * after[k];
                dp[i] += accumcount * (k+1) * after[k];

                dp[i] -= accumbefore * (k+1) * replacing[k];
                dp[i] += accumbefore * (k+1) * count[k];

                accumcount += count[k];
                accumreplacing += replacing[k];
                accumbefore += before[k];
            }

            dp[i] -= repc[i];
            dp[i] += per;

            for(int j{}; j < 26; ++j)
                changes[i][j] = count[j] - replacing[j];
        }else{
            int ne = *next_occ;

            dp[i] = dp[ne];
            ar<int, 26> after = getcountschanged(ne);
            {
                ar<int, 26> t = getcounts(i+m, ne-1);
                for(int i{}; i < 26; ++i)
                    after[i] += t[i];
            }

            ar<int, 26> replacing = getcounts(i, i+m-1);
            ar<int, 26> before = getcounts(0, i-1);
            int accumcount{};
            int accumreplacing{};
            int accumbefore{};

            for(int k{}; k < 26; ++k){
                dp[i] -= accumreplacing * (k+1) * after[k];
                dp[i] += accumcount * (k+1) * after[k];

                dp[i] -= accumbefore * (k+1) * replacing[k];
                dp[i] += accumbefore * (k+1) * count[k];

                accumcount += count[k];
                accumreplacing += replacing[k];
                accumbefore += before[k];
            }

            dp[i] -= repc[i];
            dp[i] += per;

            changes[i] = changes[ne];
            for(int j{}; j < 26; ++j)
                changes[i][j] += count[j] - replacing[j];
        }

    }


    cout << *min_element(all(dp)) << '\n';
}

uci main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int tc; cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t  << ": ";
        solve();
    }
}

/*
    random number generator stuff
    num = rng(); gives integer number
    num = uniform_int_distribution<int>(a, b)(rng); -> bounds [a, b]
    num = uniform_real_distribution<double>(a, b)(rng); -> bounds [a, b)
    can also instantiate distributions and call on generator:
    uniform_int_distribution<int> thing(a, b);
    num = thing(rng);
*/
// struct custom_hash {
//     static uint64_t splitmix64(uint64_t x) {
//         // http://xorshift.di.unimi.it/splitmix64.c
//         x += 0x9e3779b97f4a7c15;
//         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
//         x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
//         return x ^ (x >> 31);
//     }
//     size_t operator()(uint64_t x) const {
//         static const uint64_t FIXED_RANDOM = lrng();
//         return splitmix64(x + FIXED_RANDOM);
//     }
// };
