#include <bits/stdc++.h>
using namespace std;
const long long N = 400005;
const long long INF = 0x3f3f3f3f3f3f3f3f;
long long n, T, dp[2005][2005];
long long prs[30][2005], prt[30][2005];
string s, t;
long long init() {
  for (long long i = 1; i <= n; i++)
    for (long long j = i; j <= n; j++) dp[i][j] = INF;
  for (long long i = 0; i < 26; i++) prs[0][i] = prt[0][i] = 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j < 26; j++) {
      prs[j][i] = prs[j][i - 1];
      prt[j][i] = prt[j][i - 1];
    }
    prs[s[i] - 'a'][i]++;
    prt[t[i] - 'a'][i]++;
  }
  for (long long i = 0; i < 26; i++)
    if (prs[i][n] != prt[i][n]) return 1;
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> T;
  while (T--) {
    cin >> n;
    cin >> s;
    cin >> t;
    s = ' ' + s;
    t = ' ' + t;
    if (init()) {
      cout << -1 << '\n';
      continue;
    }
    for (long long i = 1; i <= n; i++) {
      for (long long j = i; j <= n; j++) {
        dp[i][j] = dp[i - 1][j] + 1;
        if (s[i] == t[j]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
        if (prs[t[j] - 'a'][i] < prt[t[j] - 'a'][j])
          dp[i][j] = min(dp[i][j], dp[i][j - 1]);
      }
    }
    cout << dp[n][n] << '\n';
  }
  return 0;
}
