#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e3 + 3;
const int ALF = 26;
int pref[MAXN][ALF];
int pref2[MAXN][ALF];
int dp[MAXN][MAXN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int z, n, x;
  bool por;
  string s1, s2;
  cin >> z;
  while (z--) {
    cin >> n;
    cin >> s1 >> s2;
    s1 = '#' + s1;
    s2 = '#' + s2;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < ALF; j++) {
        pref[i][j] = pref[i - 1][j];
        pref2[i][j] = pref2[i - 1][j];
      }
      pref[i][s1[i] - 'a']++;
      pref2[i][s2[i] - 'a']++;
    }
    por = false;
    for (int i = 0; i < ALF; i++) {
      if (pref[n][i] != pref2[n][i]) por = true;
    }
    if (por) {
      cout << "-1\n";
      continue;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        dp[i][j] = dp[i - 1][j] + 1;
        if (s1[i] == s2[j]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
        x = s2[j] - 'a';
        if (pref[n][x] - pref[i][x] > pref2[n][x] - pref2[j][x])
          dp[i][j] = min(dp[i][j], dp[i][j - 1]);
      }
    }
    cout << dp[n][n] << '\n';
  }
  return 0;
}
