#include <bits/stdc++.h>
using namespace std;
const int mod1 = 998244353, mod = 1e9 + 7;
const int MAXN = 100005, MAXM = 200010;
int dp[2020][2020];
int cntS[2020][26], cntT[2020][26];
void Solve() {
  string s, t;
  int n;
  cin >> n >> s >> t;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 26; j++) {
      cntS[i + 1][j] = cntS[i][j] + (s[i] == j + 'a');
      cntT[i + 1][j] = cntT[i][j] + (t[i] == j + 'a');
    }
  }
  for (int i = 0; i < 26; i++) {
    if (cntS[n][i] != cntT[n][i]) {
      cout << -1 << endl;
      return;
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
      if (s[i] == t[j]) {
        bool check = true;
        for (int k = 0; k < 26; k++) {
          check &= (cntS[i][k] <= cntT[j][k]);
        }
        if (check) {
          dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
        }
      }
    }
  }
  cout << n - dp[n][n] << endl;
}
signed main() {
  if (fopen(""
            ".inp",
            "r")) {
    freopen(
        ""
        ".inp",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  };
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int TC = 1;
  cin >> TC;
  while (TC--) Solve();
  return 0;
}
