#include <bits/stdc++.h>
using namespace std;
int tc, n, dp[2002][2002], vis[2002][2002], tcc, f1[2002][300], f2[2002][300];
string s, t;
int solve(int i, int j) {
  if (j + i == 0) return 0;
  int &ans = dp[i][j];
  if (vis[i][j] == tc) return ans;
  vis[i][j] = tc;
  ans = 1e5;
  if (j && f1[i + 1][t[j - 1]] > f2[j + 1][t[j - 1]]) ans = solve(i, j - 1);
  if (j && i && s[i - 1] == t[j - 1]) ans = min(ans, solve(i - 1, j - 1));
  if (i) ans = min(ans, solve(i - 1, j) + 1);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> tcc;
  while (tcc--) {
    cin >> n >> s >> t;
    tc++;
    for (int j = 'a'; j <= 'z'; j++) f1[n + 1][j] = f2[n + 1][j] = 0;
    for (int i = n; i >= 1; i--) {
      for (int j = 'a'; j <= 'z'; j++) {
        f1[i][j] = f1[i + 1][j] + (s[i - 1] == j);
        f2[i][j] = f2[i + 1][j] + (t[i - 1] == j);
      }
    }
    int ans = solve(n, n);
    if (ans >= 1e5) ans = -1;
    cout << ans << "\n";
  }
}
