#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000 + 10, mod = 998244353;
int tt, n;
int dp[maxn][maxn];
string s, t;
int main() {
  cin >> tt;
  while (tt--) {
    cin >> n >> s >> t;
    string s1 = s, t1 = t;
    sort(s1.begin(), s1.end());
    sort(t1.begin(), t1.end());
    if (s1 != t1) {
      puts("-1");
      continue;
    }
    vector<vector<int> > nums(n + 1, vector<int>(27)),
        numt(n + 1, vector<int>(27));
    for (int i = 1; i <= n; ++i) {
      nums[i] = nums[i - 1];
      numt[i] = numt[i - 1];
      nums[i][s[i - 1] - 'a' + 1]++;
      numt[i][t[i - 1] - 'a' + 1]++;
    }
    for (int i = 0; i <= n; ++i)
      for (int j = 0; j <= n; ++j) dp[i][j] = 10000;
    for (int i = 0; i <= n; ++i) dp[0][i] = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = i; j <= n; ++j) {
        if (s[i - 1] == t[j - 1])
          dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
        else {
          dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
          int c = t[j - 1] - 'a' + 1;
          if (nums[i][c] < numt[j][c]) dp[i][j] = min(dp[i][j], dp[i][j - 1]);
        }
      }
    }
    cout << dp[n][n] << "\n";
  }
  return 0;
}
