#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int f[N][N];
char s[N], t[N];
int cs[26], ct[26];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    scanf("%s%s", s + 1, t + 1);
    f[0][0] = f[0][1] = f[1][0] = 0;
    memset(cs, 0, sizeof(cs));
    memset(ct, 0, sizeof(ct));
    for (int i = 1; i <= n; i++) {
      cs[s[i] - 'a']++;
      ct[t[i] - 'a']++;
    }
    bool ok = 1;
    for (int i = 0; i < 26; i++) {
      if (cs[i] != ct[i]) ok = 0;
    }
    if (!ok) {
      cout << -1 << endl;
      continue;
    }
    int tt[N];
    for (int i = 1; i <= n; i++) {
      memcpy(tt, ct, sizeof(ct));
      for (int j = 1; j <= n; j++) {
        f[i][j] = max(f[i][j - 1], f[i - 1][j]);
        if (s[i] == t[j]) {
          bool ok = 1;
          for (int k = 0; k < 26; k++) {
            if (cs[k] < tt[k]) {
              ok = 0;
              break;
            }
          }
          if (ok) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1);
        }
        tt[t[j] - 'a']--;
      }
      cs[s[i] - 'a']--;
    }
    cout << n - f[n][n] << endl;
  }
}
