#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 10;
int dp[maxn][maxn];
char s[maxn], t[maxn];
int cnt[2][maxn][26];
int dfs(int x, int y) {
  int &now = dp[x][y];
  if (now != -1) return now;
  if (y == 0) return now = 0;
  now = INT_MAX;
  if (x > 0) {
    if (s[x] == t[y]) now = min(now, dfs(x - 1, y - 1));
    now = min(now, dfs(x - 1, y) + 1);
  }
  if (cnt[0][x + 1][t[y] - 'a'] > cnt[1][y + 1][t[y] - 'a']) {
    now = min(now, dfs(x, y - 1));
  }
  return now;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d%s%s", &n, s + 1, t + 1);
    memset(cnt, 0, sizeof cnt);
    int flag = 0;
    for (int i = n; i >= 1; --i) {
      memcpy(cnt[0][i], cnt[0][i + 1], sizeof cnt[0][i]);
      memcpy(cnt[1][i], cnt[1][i + 1], sizeof cnt[1][i]);
      ++cnt[0][i][s[i] - 'a'];
      ++cnt[1][i][t[i] - 'a'];
    }
    for (int i = 0; i < 26; ++i)
      if (cnt[0][1][i] != cnt[1][1][i]) flag |= 1;
    if (flag)
      puts("-1");
    else {
      for (int i = 1; i <= n; ++i) {
        for (int j = i; j <= n; ++j) dp[i][j] = -1;
      }
      printf("%d\n", dfs(n, n));
    }
  }
  return 0;
}
