#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10;
int cnts[256][N];
int cntt[256][N];
int n;
string s, t;
int dp[N][N];
int rec(int i, int j) {
  if (j > i) return 1e6;
  if (i == n) return 0;
  int &ans = dp[i][j];
  if (~ans) return ans;
  ans = 1e6;
  if (s[i] == t[j]) ans = min(ans, rec(i + 1, j + 1));
  ans = min(ans, 1 + rec(i + 1, j));
  if (s[i] != t[j] and cnts[t[j]][i] >= cntt[t[j]][j])
    ans = min(ans, rec(i, j + 1));
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  for (int tt = 0; tt < T; tt++) {
    cin >> n;
    cin >> s >> t;
    vector<int> f1(256), f2(256);
    for (int i = 0; i < (int)(s.size()); i++) f1[s[i]]++;
    for (int i = 0; i < (int)(t.size()); i++) f2[t[i]]++;
    if (f1 != f2) {
      cout << -1 << "\n";
      continue;
    }
    reverse((s).begin(), (s).end());
    reverse((t).begin(), (t).end());
    for (int i = 0; i < n; i++)
      for (int j = 0; j < 256; j++) cnts[j][i] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < 256; j++) cntt[j][i] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) dp[i][j] = -1;
    for (int i = 0; i < n; i++) {
      cnts[s[i]][i] = 1;
      cntt[t[i]][i] = 1;
      if (i)
        for (int j = 0; j < 256; j++) {
          cnts[j][i] += cnts[j][i - 1];
          cntt[j][i] += cntt[j][i - 1];
        }
    }
    cout << rec(0, 0) << endl;
  }
}
