#include <bits/stdc++.h>
using namespace std;
const int M = 2e3 + 7;
char s[M], t[M];
int ps[M][27], pt[M][27];
int dp[M][M];
bool ck(int i, int j) {
  for (int k = 0; k < 26; k++) {
    if (ps[i][k] < pt[j][k]) return false;
  }
  return true;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= 25; j++) ps[i][j] = pt[i][j] = 0;
    scanf("%s%s", s + 1, t + 1);
    int i = 1;
    while (i) {
      int j = n - i + 1;
      if (i > j) break;
      swap(s[i], s[j]);
      swap(t[i], t[j]);
      i++;
    }
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) dp[i][j] = 0x3f3f3f3f;
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 26; j++)
        ps[i][j] = ps[i - 1][j], pt[i][j] = pt[i - 1][j];
      ps[i][s[i] - 'a']++;
      pt[i][t[i] - 'a']++;
      dp[i][0] = i;
    }
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        if (!ck(i, j)) continue;
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
        dp[i][j] = min(dp[i][j], dp[i][j - 1]);
        if (s[i] == t[j]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
      }
    if (dp[n][n] > 1e5)
      puts("-1");
    else
      printf("%d\n", dp[n][n]);
  }
  return 0;
}
