#include <bits/stdc++.h>
using namespace std;
using INT = long long;
char A[2222], B[2222];
int cnt[33];
int SA[2222][26], SB[2222][30];
int dp[2222][2222];
int solve(int n) {
  for (int i = 0; i <= 26; i++)
    if (cnt[i]) return -1;
  for (int i = 0; i <= n + 2; i++)
    for (int j = 0; j <= n + 2; j++) dp[i][j] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 26; j++)
      SA[i][j] = SA[i - 1][j], SB[i][j] = SB[i - 1][j];
    SA[i][A[i] - 'a']++;
    SB[i][B[i] - 'a']++;
  }
  for (int i = 0; i <= n; i++) dp[0][i] = 0, dp[i][0] = -0x3f3f3f3f;
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (SA[i][A[i] - 'a'] > SB[j][A[i] - 'a']) {
        dp[i][j] = -0x3f3f3f3f;
        continue;
      }
      if (i > j) {
        dp[i][j] = -0x3f3f3f3f;
        continue;
      }
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (A[i] == B[j]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  return n - dp[n][n];
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    scanf("%s %s", A + 1, B + 1);
    memset(cnt, 0, sizeof cnt);
    for (int i = 1; i <= n; i++) {
      cnt[A[i] - 'a']++;
      cnt[B[i] - 'a']--;
    }
    printf("%d\n", solve(n));
  }
  return 0;
}
