/**
 *    author:  tourist
 *    created: 21.10.2026 20:23:12
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

template <typename T>
T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a; swap(a, m);
    u -= t * v; swap(u, v);
  }
  assert(m == 1);
  return u;
}

template <typename T>
class Modular {
 public:
  using Type = typename decay<decltype(T::value)>::type;

  constexpr Modular() : value() {}
  template <typename U>
  Modular(const U& x) {
    value = normalize(x);
  }

  template <typename U>
  static Type normalize(const U& x) {
    Type v;
    if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
    else v = static_cast<Type>(x % mod());
    if (v < 0) v += mod();
    return v;
  }

  const Type& operator()() const { return value; }
  template <typename U>
  explicit operator U() const { return static_cast<U>(value); }
  constexpr static Type mod() { return T::value; }

  Modular& operator+=(const Modular& other) { value += other.value; value -= (value >= mod()) * mod(); return *this; }
  Modular& operator-=(const Modular& other) { value -= other.value; value += (value < 0) * mod(); return *this; }
  template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
  template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
  Modular& operator++() { return *this += 1; }
  Modular& operator--() { return *this -= 1; }
  Modular operator++(int) { Modular result(*this); *this += 1; return result; }
  Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
  Modular operator-() const { return Modular(-value); }

  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
    value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
    return *this;
  }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) {
    int64_t q = int64_t(static_cast<long double>(value) * rhs.value / mod());
    value = normalize(int64_t(uint64_t(value) * uint64_t(rhs.value) - uint64_t(q) * uint64_t(mod())));
    return *this;
  }
  template <typename U = T>
  typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
    value = normalize(value * rhs.value);
    return *this;
  }

  Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }

  friend const Type& abs(const Modular& x) { return x.value; }

  template <typename U>
  friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);

  template <typename U>
  friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);

  template <typename V, typename U>
  friend V& operator>>(V& stream, Modular<U>& number);

 private:
  Type value;
};

template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }

template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }

template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }

template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }

template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }

template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }

template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }

template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
  assert(b >= 0);
  Modular<T> x = a, res = 1;
  U p = b;
  while (p > 0) {
    if (p & 1) res *= x;
    x *= x;
    p >>= 1;
  }
  return res;
}

template <typename T>
bool IsZero(const Modular<T>& number) {
  return number() == 0;
}

template <typename T>
string to_string(const Modular<T>& number) {
  return to_string(number());
}

// U == std::ostream? but done this way because of fastoutput
template <typename U, typename T>
U& operator<<(U& stream, const Modular<T>& number) {
  return stream << number();
}

// U == std::istream? but done this way because of fastinput
template <typename U, typename T>
U& operator>>(U& stream, Modular<T>& number) {
  typename common_type<typename Modular<T>::Type, int64_t>::type x;
  stream >> x;
  number.value = Modular<T>::normalize(x);
  return stream;
}

// using ModType = int;

// struct VarMod { static ModType value; };
// ModType VarMod::value;
// ModType& md = VarMod::value;
// using Mint = Modular<VarMod>;

constexpr int64_t md = (int64_t(1) << 61) - 1;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;

mt19937_64 rnd(58);
const Mint step = int64_t((md / 4) + rnd() % (md / 2));

// vector<Mint> fact(1, 1);
// vector<Mint> inv_fact(1, 1);

// Mint C(int n, int k) {
//   if (k < 0 || k > n) {
//     return 0;
//   }
//   while ((int) fact.size() < n + 1) {
//     fact.push_back(fact.back() * (int) fact.size());
//     inv_fact.push_back(1 / fact.back());
//   }
//   return fact[n] * inv_fact[k] * inv_fact[n - k];
// }

namespace seg_tree {

// Floor of log_2(a); index of highest 1-bit
inline int floor_log_2(int a) {
  return a ? (31 - __builtin_clz(a)) : -1;
}

struct point {
  int a;
  point() : a(0) {}
  explicit point(int a_) : a(a_) { assert(a >= -1); }

  explicit operator bool () { return bool(a); }

  // This is useful so you can directly do array indices
  /* implicit */ operator int() const { return a; }

  point c(bool z) const {
    return point((a << 1) | z);
  }

  point operator [] (bool z) const {
    return c(z);
  }

  point p() const {
    return point(a >> 1);
  }

  friend std::ostream& operator << (std::ostream& o, const point& p) { return o << int(p); }

  template <typename F> void for_each(F f) const {
    for (int v = a; v > 0; v >>= 1) {
      f(point(v));
    }
  }

  template <typename F> void for_parents_down(F f) const {
    // strictly greater than 0
    for (int L = floor_log_2(a); L > 0; L--) {
      f(point(a >> L));
    }
  }

  template <typename F> void for_parents_up(F f) const {
    for (int v = a >> 1; v > 0; v >>= 1) {
      f(point(v));
    }
  }

  point& operator ++ () { ++a; return *this; }
  point operator ++ (int) { return point(a++); }
  point& operator -- () { --a; return *this; }
  point operator -- (int) { return point(a--); }
};

struct range {
  int a, b;
  range() : a(1), b(1) {}
  range(int a_, int b_) : a(a_), b(b_) {
    assert(1 <= a && a <= b && b <= 2 * a);
  }
  explicit range(std::array<int, 2> r) : range(r[0], r[1]) {}

  explicit operator std::array<int, 2>() const {
    return {a, b};
  }

  const int& operator[] (bool z) const {
    return z ? b : a;
  }

  friend std::ostream& operator << (std::ostream& o, const range& r) { return o << "[" << r.a << ".." << r.b << ")"; }

  // Iterate over the range from outside-in.
  //   Calls f(point a)
  template <typename F> void for_each(F f) const {
    for (int x = a, y = b; x < y; x >>= 1, y >>= 1) {
      if (x & 1) f(point(x++));
      if (y & 1) f(point(--y));
    }
  }

  // Iterate over the range from outside-in.
  //   Calls f(point a, bool is_right)
  template <typename F> void for_each_with_side(F f) const {
    for (int x = a, y = b; x < y; x >>= 1, y >>= 1) {
      if (x & 1) f(point(x++), false);
      if (y & 1) f(point(--y), true);
    }
  }

  // Iterate over the range from left to right.
  //    Calls f(point)
  template <typename F> void for_each_l_to_r(F f) const {
    int anc_depth = floor_log_2((a - 1) ^ b);
    int anc_msk = (1 << anc_depth) - 1;
    for (int v = (-a) & anc_msk; v; v &= v - 1) {
      int i = __builtin_ctz(v);
      f(point(((a - 1) >> i) + 1));
    }
    for (int v = b & anc_msk; v; ) {
      int i = floor_log_2(v);
      f(point((b >> i) - 1));
      v ^= (1 << i);
    }
  }

  // Iterate over the range from right to left.
  //    Calls f(point)
  template <typename F> void for_each_r_to_l(F f) const {
    int anc_depth = floor_log_2((a - 1) ^ b);
    int anc_msk = (1 << anc_depth) - 1;
    for (int v = b & anc_msk; v; v &= v - 1) {
      int i = __builtin_ctz(unsigned(v));
      f(point((b >> i) - 1));
    }
    for (int v = (-a) & anc_msk; v; ) {
      int i = floor_log_2(v);
      f(point(((a - 1) >> i) + 1));
      v ^= (1 << i);
    }
  }

  template <typename F> void for_parents_down(F f) const {
    int x = a, y = b;
    if ((x ^ y) > x) { x <<= 1, std::swap(x, y); }
    int dx = __builtin_ctz(unsigned(x));
    int dy = __builtin_ctz(unsigned(y));
    int anc_depth = floor_log_2((x - 1) ^ y);
    for (int i = floor_log_2(x); i > dx; i--) {
      f(point(x >> i));
    }
    for (int i = anc_depth; i > dy; i--) {
      f(point(y >> i));
    }
  }

  template <typename F> void for_parents_up(F f) const {
    int x = a, y = b;
    if ((x ^ y) > x) { x <<= 1, std::swap(x, y); }
    int dx = __builtin_ctz(unsigned(x));
    int dy = __builtin_ctz(unsigned(y));
    int anc_depth = floor_log_2((x - 1) ^ y);
    for (int i = dx + 1; i <= anc_depth; i++) {
      f(point(x >> i));
    }
    for (int v = y >> (dy + 1); v; v >>= 1) {
      f(point(v));
    }
  }
};

struct in_order_layout {
  // Alias them in for convenience
  using point = seg_tree::point;
  using range = seg_tree::range;

  int n, s;
  in_order_layout() : n(0), s(0) {}
  in_order_layout(int n_) : n(n_) {
    s = 1;
    while (s < n) s *= 2;
  }

  point get_point(int a) const {
    assert(0 <= a && a < n);
    a += s;
    return point(a >= 2 * n ? a - n : a);
  }

  range get_range(int a, int b) const {
    assert(0 <= a && a <= b && b <= n);
    if (n == 0) return range();
    a += s, b += s;
    return range((a >= 2 * n ? 2 * (a - n) : a), (b >= 2 * n ? 2 * (b - n) : b));
  }

  range get_range(std::array<int, 2> p) const {
    return get_range(p[0], p[1]);
  }

  int get_leaf_index(point pt) const {
    int a = int(pt);
    assert(n <= a && a < 2 * n);
    return (a < s ? a + n : a) - s;
  }

  std::array<int, 2> get_node_bounds(point pt) const {
    int a = int(pt);
    assert(1 <= a && a < 2 * n);
    int l = __builtin_clz(unsigned(a)) - __builtin_clz(unsigned(2 * n - 1));
    int x = a << l, y = (a + 1) << l;
    assert(s <= x && x < y && y <= 2 * s);
    return {(x >= 2 * n ? (x >> 1) + n : x) - s, (y >= 2 * n ? (y >> 1) + n : y) - s};
  }

  int get_node_split(point pt) const {
    int a = int(pt);
    assert(1 <= a && a < n);
    int l = __builtin_clz(unsigned(2 * a + 1)) - __builtin_clz(unsigned(2 * n - 1));
    int x = (2 * a + 1) << l;
    assert(s <= x && x < 2 * s);
    return (x >= 2 * n ? (x >> 1) + n : x) - s;
  }

  int get_node_size(point pt) const {
    auto bounds = get_node_bounds(pt);
    return bounds[1] - bounds[0];
  }
};

} // namespace seg_tree

struct NoTag {};

template <typename Info, typename Tag = NoTag>
class SegmentTree {
 public:
  static constexpr bool Lazy = false;

  int n;
  vector<Info> infos;
  vector<Tag> tags;
  seg_tree::in_order_layout layout;

  class Node {
   public:
    friend class SegmentTree;
    SegmentTree* tree;
    seg_tree::point id;
    Node(SegmentTree* tree_, seg_tree::point id_) : tree(tree_), id(id_) {}

    const Info& Data() const { return tree->infos[id]; }
    bool IsLeaf() const { return id >= tree->n; }
    auto Bounds() const { return tree->layout.get_node_bounds(id); }

    std::pair<Node, Node> Children() const {
      assert(!IsLeaf());
      tree->DowndateNode(id);
      return {Node(tree, id.c(0)), Node(tree, id.c(1))};
    }
  };

  template <typename Region>
  void Prepare(const Region& region) {
    if constexpr (Lazy) {
      region.for_parents_down([&](seg_tree::point a) { DowndateNode(a); });
    }
  }

  template <typename State>
  static constexpr bool HasAppendRight = false;
  template <typename State>
  static constexpr bool HasAppendLeft = false;
  template <typename State>
  static constexpr bool HasUnite = true;

  template <typename State>
  static State ExtendRight(const State& s, Node v) {
    return s.Unite(v.Data());
  }
  template <typename State>
  static State ExtendLeft(const State& s, Node v) {
    return v.Data().Unite(s);
  }

  void Apply(seg_tree::point a, const Tag& t) {
    auto [l, r] = layout.get_node_bounds(a);
    if (!t.ApplyTo(infos[a], l, r)) {
      assert(a < n);
      DowndateNode(a);
      Apply(a.c(0), t);
      Apply(a.c(1), t);
      UpdateNode(a);
      return;
    }
    if (a < n) {
      t.ApplyTo(tags[a]);
    }
  }

  void DowndateNode(seg_tree::point a) {
    if constexpr (Lazy) {
      if (!tags[a].Empty()) {
        Apply(a.c(0), tags[a]);
        Apply(a.c(1), tags[a]);
        tags[a] = Tag();
      }
    }
  }

  void UpdateNode(seg_tree::point a) {
    Node left(this, a.c(0)), right(this, a.c(1));
    infos[a] = left.Data().Unite(right.Data());
  }

  SegmentTree() : SegmentTree(0) {}
  SegmentTree(int n_) : SegmentTree(vector<Info>(n_)) {}
  SegmentTree(const vector<Info>& a) : n(int(a.size())) {
    infos.resize(2 * n);
    if constexpr (Lazy) tags.resize(n);
    layout = seg_tree::in_order_layout(n);
    for (int i = 0; i < n; i++) {
      infos[layout.get_point(i)] = a[i];
    }
    for (int i = n - 1; i >= 1; i--) {
      UpdateNode(seg_tree::point(i));
    }
  }

  void Modify(int l, int r, const Tag& t) {
    auto rng = layout.get_range(l, r);
    if (l == r) return;
    Prepare(rng);
    rng.for_each([&](seg_tree::point a) {
      Apply(a, t);
    });
    rng.for_parents_up([&](seg_tree::point a) {
      UpdateNode(a);
    });
  }

  void Set(int p, const Info& v) {
    auto pt = layout.get_point(p);
    Prepare(pt);
    infos[pt] = v;
    pt.for_parents_up([&](seg_tree::point a) {
      UpdateNode(a);
    });
  }

  template <bool Reverse = false, typename F>
  void QueryNodes(int l, int r, F&& f) {
    auto rng = layout.get_range(l, r);
    if (l == r) return;
    Prepare(rng);
    auto visit = [&](seg_tree::point a) { f(Node(this, a)); };
    if constexpr (Reverse) {
      rng.for_each_r_to_l(visit);
    } else {
      rng.for_each_l_to_r(visit);
    }
  }

  template <typename F>
  void QueryNodesReverse(int l, int r, F&& f) {
    QueryNodes<true>(l, r, std::forward<F>(f));
  }

  template <typename State = Info>
  State Query(int l, int r, State state = {}) {
    QueryNodes(l, r, [&](Node v) { state = ExtendRight(state, v); });
    return state;
  }

  Info Get(int p) {
    auto pt = layout.get_point(p);
    Prepare(pt);
    return infos[pt];
  }

  template <typename State = Info, typename F>
  int MaxRight(int l, F f, State sum = {}) {
    int res = n;
    QueryNodes(l, n, [&](Node node) {
      if (res != n) return;
      State next = ExtendRight(sum, node);
      if (f(std::as_const(next))) {
        sum = std::move(next);
        return;
      }
      while (!node.IsLeaf()) {
        auto [left, right] = node.Children();
        next = ExtendRight(sum, left);
        if (f(std::as_const(next))) {
          sum = std::move(next);
          node = right;
        } else {
          node = left;
        }
      }
      res = node.Bounds()[0];
    });
    return res;
  }

  template <typename State = Info, typename F>
  int MinLeft(int r, F f, State sum = {}) {
    int res = 0;
    QueryNodesReverse(0, r, [&](Node node) {
      if (res != 0) return;
      State next = ExtendLeft(sum, node);
      if (f(std::as_const(next))) {
        sum = std::move(next);
        return;
      }
      while (!node.IsLeaf()) {
        auto [left, right] = node.Children();
        next = ExtendLeft(sum, right);
        if (f(std::as_const(next))) {
          sum = std::move(next);
          node = left;
        } else {
          node = right;
        }
      }
      res = node.Bounds()[1];
    });
    return res;
  }
};

array<int, 26> zero;

struct Info {
  array<int, 26> a = zero;
  int64_t cost = 0;

  Info Unite(const Info& b) const {
    Info res;
    res.cost = cost + b.cost;
    int pref = 0;
    for (int i = 0; i < 26; i++) {
      res.cost += int64_t(i + 1) * b.a[i] * pref;
      pref += a[i];
      res.a[i] = a[i] + b.a[i];
    }
    return res;
  }

  static Info GetDefault([[maybe_unused]] int l, [[maybe_unused]] int r) {
    return Info();
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  for (int i = 0; i < 26; i++) {
    zero[i] = 0;
  }
  int tt;
  cin >> tt;
  while (tt--) {
    string s, t;
    cin >> s >> t;
    int n = int(s.size());
    int m = int(t.size());
    Info vt;
    for (int i = 0; i < m; i++) {
      int c = int(t[i] - 'a');
      Info single;
      single.a[c] = 1;
      single.cost = c + 1;
      vt = vt.Unite(single);
    }
    vector<Info> init(n);
    vector<Mint> hp(n + 1);
    for (int i = 0; i < n; i++) {
      int c = int(s[i] - 'a');
      hp[i + 1] = hp[i] * step + c;
      init[i].a[c] = 1;
      init[i].cost = c + 1;
    }
    SegmentTree st(init);
    Mint pw = power(step, m);
    vector<Mint> h(n - m + 1);
    for (int i = 0; i <= n - m; i++) {
      h[i] = hp[i + m] - hp[i] * pw;
    }
    vector<Info> f(n + 1);
    map<Mint, int> mp;
    int64_t ans = int64_t(1e18);
    for (int i = n - m; i >= 0; i--) {
      if (i + m <= n - m) {
        mp[h[i + m]] = i + m;
      }
      auto it = mp.find(h[i]);
      int pos = (it != mp.end() ? it->second : n);
      f[i] = vt.Unite(st.Query(i + m, pos)).Unite(f[pos]);
      ans = min(ans, st.Query(0, i).Unite(f[i]).cost);
    }
    cout << ans << '\n';
  }
  return 0;
}
