#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

typedef uint64_t ull;
struct H {
	ull x; H(ull x=0) : x(x) {}
	H operator+(H o) { return x + o.x + (x + o.x < x); }
	H operator-(H o) { return *this + ~o.x; }
	H operator*(H o) { auto m = (__uint128_t)x * o.x;
		return H((ull)m) + (ull)(m >> 64); }
	ull get() const { return x + !~x; }
	bool operator==(H o) const { return get() == o.get(); }
	bool operator<(H o) const { return get() < o.get(); }
};
static const H C = (ll)1e11+3; // (order ~ 3e9; random also ok)

struct HashInterval {
	vector<H> ha, pw;
	HashInterval(string& str) : ha(sz(str)+1), pw(ha) {
		pw[0] = 1;
		rep(i,0,sz(str))
			ha[i+1] = ha[i] * C + str[i],
			pw[i+1] = pw[i] * C;
	}
	H hashInterval(int a, int b) { // hash [a, b)
		return ha[b] - ha[a] * pw[b - a];
	}
};

vector<H> getHashes(string& str, int length) {
	if (sz(str) < length) return {};
	H h = 0, pw = 1;
	rep(i,0,length)
		h = h * C + str[i], pw = pw * C;
	vector<H> ret = {h};
	rep(i,length,sz(str)) {
		ret.push_back(h = h * C + str[i] - pw * str[i-length]);
	}
	return ret;
}

H hashString(string& s){H h{}; for(char c:s) h=h*C+c;return h;}

void solve(){
	string S;
	string T;
	cin >> S >> T;
	int N = (int)S.size();
	int M = (int)T.size();
	vector<vector<ll> > psum(N+1, vector<ll>(26, 0));
	vector<int> val(N);
	for(int i = 0; i < N; i++) val[i] = S[i]-'a';
	for(int i = 0; i < N; i++){
		psum[i+1] = psum[i];
		psum[i+1][val[i]]++;
	}
	vector<vector<ll> > psum_by_char(N+1, vector<ll>(27, 0));
	for(int i = 0; i <= N; i++){
		for(int c = 0; c < 26; c++){
			psum_by_char[i][c+1] = psum_by_char[i][c] + psum[i][c];
		}
	}

	ll base_score = 0;
	for(int i = 0; i < N; i++){
		base_score += (val[i]+1) * (psum_by_char[i][val[i]]+1);
	}
	vector<ll> within_score(N-M+1, 0);
	for(int i = 0; i < M; i++){
		within_score[0] += (val[i]+1) * (psum_by_char[i][val[i]]+1);
	}
	for(int i = 0; i+M < N; i++){
		within_score[i+1] = within_score[i];
		within_score[i+1] += (val[i+M]+1) - (val[i]+1);
		for(int c = val[i]+1; c < 26; c++){
			within_score[i+1] -= (c+1) * (psum[i+M][c] - psum[i+1][c]);
		}
		for(int c = 0; c < val[i+M]; c++){
			within_score[i+1] += (val[i+M]+1) * (psum[i+M][c] - psum[i+1][c]);
		}
	}

	ll within_T_score = 0;
	vector<ll> tfreq(26);
	for(int i = 0; i < M; i++){
		int tval = T[i]-'a';
		for(int c = 0; c < tval; c++){
			within_T_score += (tval+1) * tfreq[c];
		}
		within_T_score += (tval+1);
		tfreq[tval]++;
	}
	vector<ll> tfreq_by_char(27);
	for(int i = 0; i < 26; i++){
		tfreq_by_char[i+1] = tfreq_by_char[i] + tfreq[i];
	}

	auto res = getHashes(S, M);
	map<H, vector<int>> hash_locs;
	for(int i = 0; i <= N-M; i++){
		hash_locs[res[i]].push_back(i);
	}
	vector<ll> scores(N-M+1);
	vector<ll> occs(N-M+1);
	for(int i = N-M; i >= 0; i--){
		auto hsh = res[i];
		auto it = lower_bound(hash_locs[hsh].begin(), hash_locs[hsh].end(), i+M);
		int nxt = (it == hash_locs[hsh].end()) ? -1 : *it;
		if(nxt >= 0){
			scores[i] = scores[nxt];
			occs[i] = occs[nxt]+1;
		} else {
			scores[i] = base_score;
			occs[i] = 1;
		}
		scores[i] += within_T_score - within_score[i];
		for(int c = 0; c < 26; c++){
			scores[i] += (c+1) * (tfreq[c] - (psum[i+M][c] - psum[i][c]))
				* (psum_by_char[i][c] - psum_by_char[0][c]);
		}
		ll extra_occs = occs[i] - 1;
		for(int c = 0; c < 26; c++){
			scores[i] += (c+1) * (tfreq_by_char[c] - (psum_by_char[i+M][c] - psum_by_char[i][c]))
				* (psum[N][c] - psum[i+M][c] + extra_occs * (tfreq[c] - (psum[i+M][c] - psum[i][c])));
		}
	}
	auto ans = *min_element(scores.begin(), scores.end());
	cout << ans << '\n';
}

int main(){
	int T;
	cin >> T;
	while(T--) solve();
}
