#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005;
char s[maxn], t[maxn];
int dp[maxn][maxn], pa[maxn][26], pb[maxn][26];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    scanf("%s%s", s + 1, t + 1);
    for (int i = 1; i <= n; ++i) s[i] -= 'a', t[i] -= 'a';
    for (int i = 1; i <= n; ++i)
      for (int j = 0; j < 26; ++j)
        pa[i][j] = pa[i - 1][j] + (s[i] == j),
        pb[i][j] = pb[i - 1][j] + (t[i] == j);
    bool flag = 1;
    for (int j = 0; j < 26; ++j)
      if (pa[n][j] != pb[n][j]) flag = 0;
    if (!flag)
      puts("-1");
    else {
      for (int j = 0; j <= n; ++j) dp[0][j] = 0;
      for (int i = 1; i <= n; ++i)
        for (int j = i; j <= n; ++j) {
          int res = 100000;
          if (s[i] == t[j]) res = min(res, dp[i - 1][j - 1]);
          res = min(res, dp[i - 1][j] + 1);
          if (pa[i][t[j]] < pb[j][t[j]]) res = min(res, dp[i][j - 1]);
          dp[i][j] = res;
        }
      cout << dp[n][n] << endl;
    }
  }
  return 0;
}
