#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma G++ optimize(2)
using namespace std;
const int MAX_N = 2e3 + 5;
char s[MAX_N], t[MAX_N];
int sufs[MAX_N][26], suft[MAX_N][26];
int dp[MAX_N][MAX_N];
int solve(int i, int j) {
  if (j == 0) return 0;
  if (~dp[i][j]) return dp[i][j];
  int res = 2e9;
  if (i > 0) {
    res = 1 + solve(i - 1, j);
    if (s[i] == t[j]) res = min(res, solve(i - 1, j - 1));
  }
  int c = t[j] - 'a';
  if (sufs[i + 1][c] > suft[j + 1][c]) res = min(res, solve(i, j - 1));
  return dp[i][j] = res;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n), scanf("%s", s + 1), scanf("%s", t + 1);
    for (register int i = 0; i <= 25; ++i) sufs[n + 1][i] = suft[n + 1][i] = 0;
    for (register int i = 0; i <= n; ++i)
      for (register int j = 0; j <= n; ++j) dp[i][j] = -1;
    for (register int i = n; i >= 1; --i) {
      for (register int j = 0; j <= 25; ++j)
        sufs[i][j] = sufs[i + 1][j], suft[i][j] = suft[i + 1][j];
      sufs[i][s[i] - 'a']++, suft[i][t[i] - 'a']++;
    }
    int ans = solve(n, n);
    if (ans > 1e9) ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}
