#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 2010;
int T, n;
char start[MAXN], goal[MAXN];
int dp[MAXN][MAXN];
int sifx[26][MAXN], gifx[26][MAXN];
int prefix[MAXN], cnt[27];
inline int f(int p, int q) {
  if (q == 0) return dp[p][q] = 0;
  if (dp[p][q] != INF) return dp[p][q];
  if (p) {
    dp[p][q] = 1 + f(p - 1, q);
    if (start[p] == goal[q]) dp[p][q] = min(dp[p][q], f(p - 1, q - 1));
  }
  if (sifx[goal[q] - 97][p + 1] > gifx[goal[q] - 97][q + 1])
    dp[p][q] = min(dp[p][q], f(p, q - 1));
  return dp[p][q];
}
int main() {
  scanf("%d", &T);
  for (int kase = 1; kase <= T; ++kase) {
    scanf("%d", &n);
    scanf("%s%s", start + 1, goal + 1);
    while (start[n] == goal[n] && n) n--;
    if (n == 0) {
      puts("0");
      continue;
    }
    memset(cnt, 0, sizeof(cnt));
    bool pd = 0;
    for (int i = 1; i <= n; ++i) cnt[start[i] - 97]++, cnt[goal[i] - 97]--;
    for (int i = 0; i < 26; ++i)
      if (cnt[i]) pd = 1;
    if (pd) {
      puts("-1");
      continue;
    }
    memset(sifx, 0, sizeof(sifx));
    memset(gifx, 0, sizeof(gifx));
    for (int i = n; i >= 0; --i)
      for (int j = 0; j < 26; ++j)
        sifx[j][i] = sifx[j][i + 1] + ((start[i] == j + 97) ? 1 : 0),
        gifx[j][i] = gifx[j][i + 1] + ((goal[i] == j + 97) ? 1 : 0);
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 0; i <= n; ++i)
      for (int j = 0; j <= n; ++j) {
        if (dp[i][j] >= INF) continue;
        if (sifx[goal[j + 1] - 97][i + 1] > gifx[goal[j + 1] - 97][j + 2])
          dp[i][j + 1] = min(dp[i][j], dp[i][j + 1]);
        dp[i + 1][j] = min(dp[i][j] + 1, dp[i + 1][j]);
        if (start[i + 1] == goal[j + 1])
          dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
      }
    printf("%d\n", dp[n][n] >= n ? n - 1 : dp[n][n]);
  }
}
