#include <bits/stdc++.h>
using namespace std;
char str0[2100];
char str1[2100];
int N[26];
int ans;
bool flag;
int dp[2100][2100];
int ss[26][2100], st[26][2100];
int main() {
  int T;
  int n, c, t0, t1;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    scanf("%s", str0);
    scanf("%s", str1);
    memset(ss, 0, sizeof(ss));
    memset(st, 0, sizeof(st));
    for (int i = 0; i < 26; i++) {
      N[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      t0 = str0[i] - 'a';
      t1 = str1[i] - 'a';
      N[t0]++;
      N[t1]--;
      for (int k = 0; k < 26; k++) {
        ss[k][i + 1] = ss[k][i];
        st[k][i + 1] = st[k][i];
      }
      ss[t0][i + 1]++;
      st[t1][i + 1]++;
    }
    flag = true;
    for (int i = 0; i < 26; i++) {
      if (N[i]) flag = false;
    }
    if (flag) {
      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 (str0[i - 1] == str1[j - 1]) {
            dp[i][j] = dp[i - 1][j - 1];
          } else {
            dp[i][j] = n;
            t0 = str0[i - 1] - 'a';
            t1 = str1[j - 1] - 'a';
            if (ss[t0][i] > st[t0][j]) {
              continue;
            }
            dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j]);
            if (ss[t1][i] < st[t1][j]) {
              dp[i][j] = min(dp[i][j], dp[i][j - 1]);
            }
          }
        }
      ans = dp[n][n];
    } else {
      ans = -1;
    }
    cout << ans << endl;
  }
  return 0;
}
