#include <bits/stdc++.h>
const int MAXN = 2000 + 5;
char s[MAXN], t[MAXN];
int n;
int f[MAXN][MAXN];
int p1[MAXN][26], p2[MAXN][26];
inline void Solve() {
  scanf("%d", &n);
  scanf("%s%s", s + 1, t + 1);
  std::reverse(s + 1, s + n + 1);
  std::reverse(t + 1, t + n + 1);
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= 25; ++j)
      p1[i][j] = p1[i - 1][j], p2[i][j] = p2[i - 1][j];
    p1[i][s[i] - 'a']++;
    p2[i][t[i] - 'a']++;
  }
  for (int i = 0; i <= 25; ++i) {
    if (p1[n][i] != p2[n][i]) {
      puts("-1");
      return;
    }
  }
  for (int i = 0; i <= n + 1; ++i)
    for (int j = 0; j <= n + 1; ++j) f[i][j] = 1e9;
  for (int i = 0; i <= n; ++i) f[i][0] = i;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= i; ++j) {
      bool flag = true;
      for (int k = 0; k <= 25; ++k) flag &= p1[i][k] >= p2[j][k];
      if (!flag) continue;
      if (s[i] == t[j]) f[i][j] = std::min(f[i][j], f[i - 1][j - 1]);
      f[i][j] = std::min(f[i][j], f[i - 1][j] + 1);
      f[i][j] = std::min(f[i][j], f[i][j - 1]);
    }
  }
  printf("%d\n", f[n][n]);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) Solve();
  return 0;
}
