#include <bits/stdc++.h>
using namespace std;
string a, b;
int n, q, i, j, f[2001][2001], ts[26], cnta[2001][26], cntb[2001][26];
const int oo = round(1e9);
int main() {
  cin >> q;
  while (q--) {
    cin >> n >> a >> b;
    memset(ts, 0, sizeof(ts));
    for (i = 0; i < n; i++) {
      ts[a[i] - 'a']++;
      ts[b[i] - 'a']--;
    }
    bool kt = 1;
    for (i = 0; i < 26; i++)
      if (ts[i] != 0) kt = 0;
    if (kt == 0) {
      cout << -1 << '\n';
      continue;
    }
    a = " " + a;
    b = " " + b;
    memset(cnta, 0, sizeof(cnta));
    memset(cntb, 0, sizeof(cntb));
    for (i = n; i >= 1; i--) {
      for (j = 0; j < 26; j++) {
        cnta[i][j] = cnta[i + 1][j];
        cntb[i][j] = cntb[i + 1][j];
      }
      cnta[i][a[i] - 'a']++;
      cntb[i][b[i] - 'a']++;
    }
    for (i = 0; i <= n; i++)
      for (j = 0; j <= n; j++) f[i][j] = oo;
    for (i = 0; i <= n; i++)
      for (j = 0; j <= n; j++)
        if (i == 0 && j == 0)
          f[i][j] = 0;
        else {
          if (j == 0) continue;
          int x = b[j] - 'a';
          if (cnta[i + 1][x] < cntb[j + 1][x]) continue;
          if (cnta[i + 1][x] > cntb[j + 1][x]) {
            f[i][j] = min(f[i][j], f[i][j - 1] + 1);
          }
          if (i > 0) {
            if (a[i] == b[j]) f[i][j] = min(f[i][j], f[i - 1][j - 1]);
            f[i][j] = min(f[i][j], f[i - 1][j]);
          }
        }
    cout << f[n][n] << '\n';
  }
}
