#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000 + 2;
int suf_a[MAXN][26];
int suf_b[MAXN][26];
int f[MAXN][MAXN];
int main() {
  int testcases;
  cin >> testcases;
  memset(suf_a, 0, sizeof(suf_a));
  memset(suf_b, 0, sizeof(suf_b));
  memset(f, 0, sizeof(f));
  while (testcases--) {
    int n;
    string a, b;
    cin >> n >> a >> b;
    a = "#" + a;
    b = "$" + b;
    for (int i = n; i >= 1; --i) {
      for (int j = 0; j < 26; ++j) {
        suf_a[i][j] = suf_a[i + 1][j];
        suf_b[i][j] = suf_b[i + 1][j];
      }
      char ca = a[i] - 'a';
      char cb = b[i] - 'a';
      suf_a[i][ca]++;
      suf_b[i][cb]++;
    }
    bool flag = true;
    for (int i = 0; i < 26 && flag; ++i)
      if (suf_a[1][i] != suf_b[1][i]) flag = false;
    if (!flag) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j) {
        f[i][j] = max(f[i - 1][j], f[i][j - 1]);
        if (a[i] == b[j]) {
          bool flag = true;
          for (int k = 0; k < 26 && flag; ++k)
            if (suf_a[i + 1][k] < suf_b[j + 1][k]) {
              flag = false;
            }
          if (flag) f[i][j] = max(f[i - 1][j - 1] + 1, f[i][j]);
        }
      }
    cout << n - f[n][n] << endl;
  }
  return 0;
}
