#include <array>
#include <cassert>
#include <iostream>
#include <map>
#include <utility>
#include <vector>

using namespace std;

template <int MOD_> struct modnum {
  static constexpr int MOD = MOD_;
  static_assert(MOD_ > 0, "MOD must be positive");

private:
  using ll = long long;

  int v;

  static int minv(int a, int m) {
    a %= m;
    assert(a);
    return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
  }

public:

  modnum() : v(0) {}
  modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
  explicit operator int() const { return v; }
  friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
  friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }

  friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
  friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }

  modnum inv() const {
    modnum res;
    res.v = minv(v, MOD);
    return res;
  }
  modnum neg() const {
    modnum res;
    res.v = v ? MOD-v : 0;
    return res;
  }

  modnum operator- () const {
    return neg();
  }
  modnum operator+ () const {
    return modnum(*this);
  }

  modnum& operator ++ () {
    v ++;
    if (v == MOD) v = 0;
    return *this;
  }
  modnum& operator -- () {
    if (v == 0) v = MOD;
    v --;
    return *this;
  }
  modnum& operator += (const modnum& o) {
    v += o.v;
    if (v >= MOD) v -= MOD;
    return *this;
  }
  modnum& operator -= (const modnum& o) {
    v -= o.v;
    if (v < 0) v += MOD;
    return *this;
  }
  modnum& operator *= (const modnum& o) {
    v = int(ll(v) * ll(o.v) % MOD);
    return *this;
  }
  modnum& operator /= (const modnum& o) {
    return *this *= o.inv();
  }

  friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
  friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
  friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
  friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
  friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
  friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};

template <typename T> T pow(T a, long long b) {
  assert(b >= 0);
  T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}

struct rabinkarp {
  typedef modnum<998244353> mnum1;
  typedef modnum<1000000007> mnum2;
  typedef pair<mnum1, mnum2> shash;
  int STRING_SZ;
  const mnum1 HASH1 = 3137;
  const mnum2 HASH2 = 1009;
  vector<shash> hashes;
  vector<mnum1> hash1pows;
  vector<mnum2> hash2pows;
  rabinkarp() {}
  rabinkarp(const string& s) {
    STRING_SZ = s.size();
    hash1pows.resize(STRING_SZ+1);
    hash1pows[0] = 1;
    for(int i = 1; i <= STRING_SZ; i++) hash1pows[i] = hash1pows[i-1] * HASH1;
    hash2pows.resize(STRING_SZ+1);
    hash2pows[0] = 1;
    for(int i = 1; i <= STRING_SZ; i++) hash2pows[i] = hash2pows[i-1] * HASH2;
    hashes.resize(STRING_SZ+1);
    for(int i = 0; i < s.size(); i++) {
      hashes[i+1].first = hashes[i].first * HASH1 + s[i];
      hashes[i+1].second = hashes[i].second * HASH2 + s[i];
    }
  }
  inline array<int, 2> getHash(int lhs, int rhs) {
    // [lhs, rhs)
    mnum1 a = hashes[rhs].first - hashes[lhs].first * hash1pows[rhs-lhs];
    mnum2 b = hashes[rhs].second - hashes[lhs].second * hash2pows[rhs-lhs];
    return {(int)a, (int)b};
  }
  inline array<int, 2> hashStr(const string& s) {
    mnum1 a = 0;
    mnum2 b = 0;
    for(auto x: s) {
      a = a * HASH1 + x;
      b = b * HASH2 + x;
    }
    return {(int)a, (int)b};
  }
};

int64_t abcost(const array<int, 26>& a, const array<int, 26>& b) {
  int64_t ret = 0;
  int lhs = 0;
  for(int i = 0; i < 26; i++) {
    ret += (i + 1) * (int64_t)b[i] * lhs;
    lhs += a[i];
  }
  return ret;
}

int64_t getCost(const string& s) {
  array<int, 26> have; have.fill(0);
  int64_t ret = 0;
  for(int i = 0; i < s.size(); i++) {
    int lhs = 1;
    for(int j = 0; j < s[i]-'a'; j++) lhs += have[j];
    ret += (s[i]-'a'+1LL)*lhs;
    have[s[i]-'a']++;
  }
  return ret;
}

void solve() {
  string s, t;
  cin >> s >> t;
  int n = s.size();
  int m = t.size();
  rabinkarp rks(s);
  array<int, 2> want = rks.hashStr(t);
  map<array<int, 2>, vector<int>> hashes;
  for(int i = 0; i + m <= n; i++) hashes[rks.getHash(i, i+m)].push_back(i);
  array<vector<int>, 26> freq;
  for(auto& x: freq) x.resize(n+1);
  for(int i = 0; i < 26; i++) {
    for(int j = 0; j < n; j++) {
      freq[i][j+1] = freq[i][j] + (s[j]-'a' == i);
    }
  }
  vector<int64_t> pcost(n+1);
  {
    array<int, 26> have; have.fill(0);
    for(int i = 0; i < n; i++) {
      int idx = s[i] - 'a';
      int amt = 0;
      for(int j = 0; j < idx; j++) amt += have[j];
      pcost[i+1] = pcost[i] + (idx + 1) * (amt + 1);
      have[idx]++;
    }
  }
  vector<int64_t> singlechange(n-m+1);
  int64_t scost = getCost(s);
  int64_t tcost = getCost(t);
  array<int, 26> tfreq; tfreq.fill(0);
  for(auto out: t) tfreq[out-'a']++;
  for(int i = 0; i < singlechange.size(); i++) {
    array<int, 26> lhs, mid, rhs;
    for(int j = 0; j < 26; j++) {
      lhs[j] = freq[j][i];
      mid[j] = freq[j][i+m] - freq[j][i];
      rhs[j] = freq[j].back() - freq[j][i+m];
    }
    singlechange[i] = tcost + abcost(lhs, tfreq) - (pcost[i+m] - pcost[i]) + abcost(tfreq, rhs) - abcost(mid, rhs);
  }
  int64_t ret = 1e18;
  for(const auto& [_, indices]: hashes) {
    array<int, 26> cfreq; cfreq.fill(0);
    for(int i = 0; i < 26; i++) cfreq[i] = freq[i][indices[0] + t.size()] - freq[i][indices[0]];
    vector<int> nxt(indices.size(), indices.size());
    int lhs = 0;
    for(int i = 0; i < indices.size(); i++) {
      while(indices[i] - indices[lhs] >= m) nxt[lhs++] = i;
    }
    vector<int64_t> dp(indices.size()+1);
    vector<int> replacecnt(indices.size()+1);
    for(int i = indices.size()-1; i >= 0; i--) {
      replacecnt[i] = replacecnt[nxt[i]] + 1;
      dp[i] = singlechange[indices[i]] + dp[nxt[i]] + replacecnt[nxt[i]] * (abcost(tfreq, tfreq) - abcost(tfreq, cfreq) - abcost(cfreq, tfreq) + abcost(cfreq, cfreq));
      ret = min(ret, dp[i]+scost);
    }
  }
  cout << ret << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while(t--) solve();
}
