#include <bits/stdc++.h>
using namespace std;
const int MX = 1000000;
int dp[2001][2001] = {}, cnt[26] = {};
bool pos[2001][2001] = {};
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    string s, t, a, b;
    cin >> n;
    cin >> s >> t;
    a = s;
    b = t;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    if (a == b) {
      for (int i = 0; i < 26; i++) {
        cnt[i] = 0;
      }
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          if (s[i - 1] == t[j - 1]) {
            pos[i][j] = 1;
          } else {
            pos[i][j] = 0;
          }
        }
      }
      for (int i = 1; i <= n; i++) {
        dp[0][i] = MX;
      }
      dp[0][0] = 0;
      for (int i = 1; i <= n; i++) {
        int ar[26] = {}, mn = MX;
        cnt[s[i - 1] - 'a']++;
        for (int j = 0; j <= n; j++) {
          dp[i][j] = MX;
          if (j) {
            ar[t[j - 1] - 'a']++;
          }
          if (pos[i][j]) dp[i][j] = mn;
          if (ar[s[i - 1] - 'a'] >= cnt[s[i - 1] - 'a'])
            dp[i][j] = min(dp[i][j], dp[i - 1][j]);
          mn = min(mn + 1, dp[i - 1][j]);
        }
      }
      cout << dp[n][n] << "\n";
    } else {
      cout << "-1\n";
    }
  }
  return 0;
}
