#include <bits/stdc++.h>
using namespace std;
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> S) {
  os << "{ ";
  for (auto s : S) os << s << " ";
  return os << "}";
}
template <class T>
ostream& operator<<(ostream& os, multiset<T> S) {
  os << "{ ";
  for (auto s : S) os << s << " ";
  return os << "}";
}
template <class L, class R>
ostream& operator<<(ostream& os, map<L, R> M) {
  os << "{ ";
  for (auto m : M) os << "(" << m.F << ":" << m.S << ") ";
  return os << "}";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " |";
  __f(comma + 1, args...);
}
const int mod = 1e7 + 7;
const int N = 2e5;
inline int fast_expo(int base, int power, int modulo = mod) {
  base %= modulo;
  if (base < 0) base += modulo;
  long long x = base, cnt = power, ans = 1;
  while (cnt) {
    if (cnt & 1) ans = (ans * x) % modulo;
    x = (x * x) % modulo;
    cnt >>= 1;
  }
  return ans;
}
inline int inv(int base, int modulo = mod) {
  return fast_expo(base, modulo - 2, modulo);
}
void solve() {
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  string ss = s, st = t;
  sort(ss.begin(), ss.end());
  sort(st.begin(), st.end());
  if (ss != st) {
    cout << "-1\n";
    return;
  }
  vector<int> pos[26];
  for (int i = 0; i < n; ++i) {
    pos[s[i] - 'a'].push_back(i);
  }
  vector<vector<int> > dp(n, vector<int>(n, -1));
  for (int i = 0; i < n; ++i) {
    dp[i][i] = pos[t[i] - 'a'][0];
  }
  int ans = 1;
  for (int l = 2; l <= n; ++l) {
    for (int i = 0; i + l - 1 < n; ++i) {
      int j = i + l - 1;
      if (dp[i][j - 1] == -1) continue;
      if (pos[t[j] - 'a'].back() <= dp[i][j - 1]) continue;
      dp[i][j] = *lower_bound(pos[t[j] - 'a'].begin(), pos[t[j] - 'a'].end(),
                              dp[i][j - 1] + 1);
      ans = l;
    }
  }
  cout << n - ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1, tc = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
