#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
int cache[2005][2005];
char s1[2005], s2[2005];
int suf1[2005][30], suf2[2005][30];
int dfs(int l, int r) {
  if (r == 0) return 0;
  int &ans = cache[l][r];
  if (ans != -1) return ans;
  char c2 = s2[r] - 'a', c1 = s1[l] - 'a';
  ans = 0x3f3f3f3f;
  if (suf1[l + 1][c2] > suf2[r + 1][c2]) ans = min(ans, dfs(l, r - 1));
  if (l) {
    ans = min(ans, dfs(l - 1, r) + 1);
    if (c1 == c2) ans = min(ans, dfs(l - 1, r - 1));
  }
  return ans;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    scanf("%s%s", s1 + 1, s2 + 1);
    for (int i = 0; i <= n + 1; i++)
      for (int j = 0; j <= n + 1; j++) cache[i][j] = -1;
    for (int i = 0; i <= 'z' - 'a'; i++) suf1[n + 1][i] = suf2[n + 1][i] = 0;
    for (int i = n; i >= 1; i--) {
      for (int j = 0; j <= 'z' - 'a'; j++) {
        suf1[i][j] = suf1[i + 1][j];
        suf2[i][j] = suf2[i + 1][j];
      }
      suf1[i][s1[i] - 'a']++;
      suf2[i][s2[i] - 'a']++;
    }
    int ok = 1;
    for (int i = 0; i <= 'z' - 'a'; i++)
      if (suf1[1][i] != suf2[1][i]) ok = 0;
    if (!ok)
      printf("-1\n");
    else {
      int ans = dfs(n, n);
      printf("%d\n", ans);
    }
  }
  return 0;
}
