#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && (c != '-')) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
const int N = 2010;
int n, pre1[N][26], pre2[N][26], f[N][N];
char s[N], t[N];
inline void solve() {
  n = read();
  scanf("%s%s", s + 1, t + 1);
  for (register int i = (1); i <= (n); i++) {
    for (register int j = (0); j <= (25); j++)
      pre1[i][j] = pre1[i - 1][j], pre2[i][j] = pre2[i - 1][j];
    pre1[i][s[i] - 'a']++, pre2[i][t[i] - 'a']++;
  }
  for (register int i = (0); i <= (25); i++)
    if (pre1[n][i] != pre2[n][i]) return puts("-1"), void(0);
  for (register int i = (1); i <= (n); i++)
    for (register int j = (1); j <= (n); j++) f[i][j] = 1e8;
  for (register int i = (1); i <= (n); i++)
    for (register int j = (1); j <= (n); j++) {
      if (i <= j && s[i] == t[j]) f[i][j] = min(f[i][j], f[i - 1][j - 1]);
      if (pre1[i][s[i] - 'a'] <= pre2[j][s[i] - 'a'])
        f[i][j] = min(f[i][j], f[i - 1][j] + 1);
      if (j > 1) f[i][j] = min(f[i][j], f[i][j - 1]);
    }
  printf("%d\n", f[n][n]);
}
int main() {
  int T = read();
  while (T--) solve();
}
