#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int cnt1[N][30], cnt2[N][30], f[N][N];
char s[N], t[N];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
      for (int j = 0; j < 26; ++j) cnt1[i][j] = 0, cnt2[i][j] = 0;
    scanf("%s", s + 1);
    getchar();
    scanf("%s", t + 1);
    getchar();
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j < 27; ++j) {
        cnt1[i][j] = cnt1[i - 1][j];
        cnt2[i][j] = cnt2[i - 1][j];
      }
      ++cnt1[i][s[i] - 'a'];
      ++cnt2[i][t[i] - 'a'];
    }
    int ok = 1;
    for (int i = 0; i < 27; ++i)
      if (cnt1[n][i] != cnt2[n][i]) {
        ok = 0;
        break;
      }
    if (!ok) {
      puts("-1");
      continue;
    }
    memset(f, 0x3f, sizeof f);
    for (int i = 0; i <= n; ++i) f[0][i] = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = i; j <= n; ++j) {
        int& v = f[i][j];
        v = min(v, f[i - 1][j] + 1);
        if (s[i] == t[j]) v = min(v, f[i - 1][j - 1]);
        if (cnt1[i][t[j] - 'a'] < cnt2[j][t[j] - 'a']) v = min(v, f[i][j - 1]);
      }
    }
    printf("%d\n", f[n][n]);
  }
  return 0;
}
