#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2005;
int T, n, cnt[2][MAXN][26], dp[MAXN][MAXN];
char s[MAXN], t[MAXN];
void Work() {
  scanf("%d", &n);
  scanf("%s%s", s + 1, t + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 26; j++) {
      for (int k = 0; k < 2; k++) {
        cnt[k][i][j] = cnt[k][i - 1][j];
      }
    }
    cnt[0][i][s[i] - 'a']++;
    cnt[1][i][t[i] - 'a']++;
  }
  for (int i = 0; i < 26; i++)
    if (cnt[0][n][i] != cnt[1][n][i]) {
      puts("-1");
      return;
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) dp[i][j] = 1e9;
  for (int i = 0; i <= n; i++) dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      dp[i][j] = dp[i - 1][j] + 1;
      if (s[i] == t[j]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
      if (cnt[0][i][t[j] - 'a'] < cnt[1][j][t[j] - 'a'])
        dp[i][j] = min(dp[i][j], dp[i][j - 1]);
    }
  }
  printf("%d\n", dp[n][n]);
}
int main() {
  scanf("%d", &T);
  while (T--) Work();
  return 0;
}
