#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000 + 5;
const int mod = 1e9 + 7;
long long qpow(long long a, long long b) {
  long long res = 1;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int dp[maxn][maxn];
int cnts[26][maxn], cntt[26][maxn];
char s[maxn], t[maxn];
int solve(int i, int j) {
  if (i == 0 || j == 0) return 0;
  int &ans = dp[i][j];
  if (ans != -1) return ans;
  ans = 1e9;
  if (i > 0) {
    ans = 1 + solve(i - 1, j);
    if (s[i] == t[j]) {
      ans = min(ans, solve(i - 1, j - 1));
    }
  }
  int ch = t[j] - 'a';
  if (cnts[ch][i + 1] - cntt[ch][j + 1] > 0) ans = min(ans, solve(i, j - 1));
  return ans;
}
int main() {
  int cs;
  scanf("%d", &cs);
out:
  while (cs--) {
    int n;
    scanf("%d", &n);
    scanf("%s%s", s + 1, t + 1);
    for (int i = 1; i <= n + 1; ++i) {
      for (int j = 0; j < 26; ++j) cnts[j][i] = cntt[j][i] = 0;
      for (int j = 1; j <= n + 1; ++j) dp[i][j] = -1;
    }
    for (int i = n; i >= 1; --i) {
      for (int j = 0; j < 26; ++j) {
        cnts[j][i] = cnts[j][i + 1];
        cntt[j][i] = cntt[j][i + 1];
      }
      cnts[s[i] - 'a'][i]++;
      cntt[t[i] - 'a'][i]++;
    }
    for (int i = 0; i < 26; ++i) {
      if (cnts[i][1] != cntt[i][1]) {
        printf("-1\n");
        goto out;
      }
    }
    printf("%d\n", solve(n, n));
  }
  return 0;
}
