#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1.0);
int dp[2005][2005];
int f[2005][26][2];
void solve() {
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  string st, tt;
  st = s;
  tt = t;
  sort(st.begin(), st.end());
  sort(tt.begin(), tt.end());
  if (st != tt) {
    cout << -1 << '\n';
    return;
  }
  for (int i = 0; i < n + 5; ++i) {
    for (int j = 0; j < n + 5; ++j) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i < n + 5; ++i) {
    for (int j = 0; j < 26; ++j) {
      for (int k = 0; k < 2; ++k) {
        f[i][j][k] = 0;
      }
    }
  }
  f[0][s[0] - 'a'][0]++;
  f[0][t[0] - 'a'][1]++;
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < 26; ++j) {
      for (int k = 0; k < 2; ++k) {
        f[i][j][k] = f[i - 1][j][k];
      }
    }
    f[i][s[i] - 'a'][0]++;
    f[i][t[i] - 'a'][1]++;
  }
  for (int i = 0; i < n + 1; ++i) {
    for (int j = 0; j < n + 1; ++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 (i < n and j < n and s[i] == t[j]) {
        int flag = 1;
        for (int k = 0; k < 26; ++k) {
          if (f[i][k][0] > f[j][k][1]) {
            flag = 0;
            break;
          }
        }
        if (flag) {
          dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
        }
      }
    }
  }
  cout << n - dp[n][n] << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
