#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar();
  return x * f;
}
char s[N], t[N];
int main() {
  int T = read();
  while (T--) {
    int n = read();
    scanf("%s", s + 1);
    scanf("%s", t + 1);
    vector<vector<int> > dp(n + 1, vector<int>(n + 1));
    vector<vector<int> > sc(n + 2, vector<int>(26));
    vector<vector<int> > tc(n + 2, vector<int>(26));
    for (int i = n; i >= 1; i--) {
      sc[i][s[i] - 'a']++, tc[i][t[i] - 'a']++;
      for (int j = 0; j < 26; j++)
        sc[i][j] += sc[i + 1][j], tc[i][j] += tc[i + 1][j];
    }
    int flag = 1;
    for (int j = 0; j < 26; j++)
      if (sc[1][j] != tc[1][j]) flag = 0;
    if (!flag) {
      puts("-1");
      continue;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
        if (s[i] == t[j]) {
          int ok = 1;
          for (int t = 0; t < 26; t++)
            if (sc[i][t] < tc[j][t]) ok = 0;
          if (ok) dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]);
        }
      }
    }
    cout << n - dp[n][n] << endl;
  }
}
