#include <bits/stdc++.h>
using namespace std::chrono;
using namespace std;
const long long mod = 1000000007;
const double pi = 3.14159265359;
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
string s, g;
long long n;
map<char, long long> mp;
long long dp[2002][2002];
long long fun(long long i, long long j) {
  if (i == -1) {
    long long s = 0;
    for (auto it : mp) {
      s += it.second;
    }
    return s;
  }
  if (dp[i][j] != -1)
    return dp[i][j];
  else if (s[i] == g[j])
    return dp[i][j] = fun(i - 1, j - 1);
  else {
    mp[s[i]]++;
    long long ret = fun(i - 1, j);
    mp[s[i]]--;
    if (mp[g[j]] > 0) {
      mp[g[j]]--;
      ret = min(ret, fun(i, j - 1) + 1);
      mp[g[j]]++;
    }
    return dp[i][j] = ret;
  }
}
int32_t main() {
  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    cin >> n;
    cin >> s >> g;
    mp.clear();
    for (long long i = 0; i <= n; i++)
      for (long long j = 0; j <= n; j++) dp[i][j] = -1;
    bool possi = 1;
    vector<long long> alp(26);
    for (long long i = 0; i < n; i++) {
      alp[s[i] - 'a']++;
      alp[g[i] - 'a']--;
    }
    for (long long i = 0; i < 26; i++) {
      if (alp[i] != 0) {
        possi = 0;
        break;
      }
    }
    if (possi)
      cout << fun(n - 1, n - 1) << endl;
    else
      cout << -1 << endl;
  }
}
