#include <bits/stdc++.h>
using namespace std;
const int maxn = 2002;
int T, n;
char s[maxn], t[maxn];
int cache[maxn][maxn];
int sufs[maxn][26], suft[maxn][26];
int dp(int i, int j) {
  if (~cache[i][j]) return cache[i][j];
  int res = 2e9;
  if (i) {
    res = 1 + dp(i - 1, j);
    if (s[i] == t[j]) {
      res = min(res, dp(i - 1, j - 1));
    }
    int ch = t[j] - 'a';
    if (sufs[i + 1][ch] > suft[j + 1][ch]) {
      res = min(res, dp(i, j - 1));
    }
  }
  return cache[i][j] = res;
}
int main() {
  cin >> T;
  while (T--) {
    cin >> n >> (s + 1) >> (t + 1);
    for (int j = 0; j <= n; j++) cache[0][j] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        cache[i][j] = -1;
      }
    }
    for (int i = 0; i < 26; i++) {
      sufs[n + 1][i] = 0;
      suft[n + 1][i] = 0;
    }
    for (int i = n; i; i--) {
      for (int j = 0; j < 26; j++) {
        sufs[i][j] = sufs[i + 1][j];
        suft[i][j] = suft[i + 1][j];
      }
      sufs[i][(int)(s[i] - 'a')]++;
      suft[i][(int)(t[i] - 'a')]++;
    }
    bool f = true;
    for (int i = 0; i < 26; i++) {
      if (sufs[1][i] != suft[1][i]) {
        f = false;
        break;
      }
    }
    int ans = (f ? dp(n, n) : -1);
    cout << ans << endl;
  }
}
