#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000, 1024000000")
#pragma GCC optimize(3)
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
const int MAXN = 2e3 + 5;
const long long MOD = 998244353LL;
char s[2][MAXN];
int dp[MAXN][MAXN];
int suf[2][MAXN][26];
int main() {
  int T;
  scanf("%d", &T);
  for (int cs = 1; cs <= T; ++cs) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < 2; ++i) {
      scanf("%s", s[i] + 1);
      for (int j = 0; j < 26; ++j) {
        suf[i][n + 1][j] = 0;
      }
      for (int j = n; j >= 1; --j) {
        for (int k = 0; k < 26; ++k) {
          suf[i][j][k] = suf[i][j + 1][k];
        }
        ++suf[i][j][s[i][j] - 'a'];
      }
    }
    for (int i = 0; i <= n; ++i) {
      for (int j = 0; j <= n; ++j) {
        dp[i][j] = INF;
      }
    }
    for (int i = 0; i <= n; ++i) {
      for (int j = i; j <= n; ++j) {
        if (j == 0) {
          dp[i][j] = 0;
        } else {
          if (i > 0) {
            if (s[0][i] == s[1][j]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
          }
          int ch = s[1][j] - 'a';
          if (suf[0][i + 1][ch] > suf[1][j + 1][ch])
            dp[i][j] = min(dp[i][j], dp[i][j - 1]);
        }
      }
    }
    int ans = dp[n][n];
    if (ans == INF) ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}
