#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T, n;
  string s, t;
  cin >> T;
  while (T--) {
    cin >> n >> s >> t;
    reverse(s.begin(), s.end());
    reverse(t.begin(), t.end());
    s = '.' + s;
    t = '.' + t;
    vector<vector<int> > dp(n + 1, vector<int>(n + 1));
    vector<vector<int> > pre_s(n + 2, vector<int>(26)),
        pre_t(n + 2, vector<int>(26));
    for (int i = 1; i <= n; i++) {
      for (int k = 0; k <= 25; k++)
        pre_s[i][k] = pre_s[i - 1][k], pre_t[i][k] = pre_t[i - 1][k];
      ++pre_s[i][s[i] - 'a'];
      ++pre_t[i][t[i] - 'a'];
    }
    bool ok = 1;
    for (int i = 0; i <= 25; i++)
      if (pre_s[n][i] != pre_t[n][i]) ok = 0;
    if (!ok) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++) dp[i][j] = 0X3F3F3F;
    for (int i = 0; i <= n; i++) dp[i][0] = i;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= i; j++) {
        bool ok = 1;
        for (int k = 0; k <= 25; k++)
          if (pre_s[i][k] < pre_t[j][k]) ok = 0;
        if (!ok) continue;
        dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1]);
        if (s[i] == t[j]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
      }
    cout << dp[n][n] << endl;
  }
}
