#include <bits/stdc++.h>
using namespace std;
int ini() {
  int x;
  scanf("%d", &x);
  return x;
}
long long inl() {
  long long x;
  scanf("%lld", &x);
  return x;
}
void file_mamager() {}
char ss[20005], tt[20005];
int scn[30][20005], tcn[30][20005];
int main() {
  int t = ini();
  while (t--) {
    int n = ini();
    for (int i = 1; i <= n; i++) {
      cin >> ss[i];
      for (int j = 0; j < 26; j++) {
        if (j == (ss[i] - 'a'))
          scn[j][i] = 1;
        else
          scn[j][i] = 0;
        scn[j][i] += scn[j][i - 1];
      }
    }
    for (int i = 1; i <= n; i++) {
      cin >> tt[i];
      for (int j = 0; j < 26; j++) {
        if (j == (tt[i] - 'a'))
          tcn[j][i] = 1;
        else
          tcn[j][i] = 0;
        tcn[j][i] += tcn[j][i - 1];
      }
    }
    bool possible = true;
    for (int j = 0; j < 26; j++)
      if (scn[j][n] != tcn[j][n]) possible = false;
    if (!possible) {
      cout << "-1\n";
      continue;
    }
    vector<vector<int> > dp(n + 5, vector<int>(n + 5));
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) dp[i][j] = (i ? 1e8 : 0);
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (ss[i] == tt[j]) dp[i][j] = dp[i - 1][j - 1];
        if (scn[ss[i] - 'a'][i - 1] < tcn[ss[i] - 'a'][j])
          dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
        if (i <= j - 1) dp[i][j] = min(dp[i][j], dp[i][j - 1]);
      }
    }
    cout << dp[n][n] << "\n";
  }
  return 0;
}
