#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const long long maxn = (long long)3e5 + 5;
const long long mod = 998244353;
const long long inf = 0x3f3f3f3f;
long long T = 1;
long long last[26];
long long dp[105][105];
char s[105], t[105];
long long num1[26], num2[26];
void solve() {
  for (long long i = 0; i <= 25; ++i) num1[i] = num2[i] = 0;
  long long n;
  cin >> n;
  cin >> (s + 1) >> (t + 1);
  for (long long i = 1; i <= n; ++i) {
    ++num1[s[i] - 'a'], ++num2[t[i] - 'a'];
    for (long long j = 0; j <= n; ++j) dp[i][j] = 0;
  }
  for (long long i = 0; i <= 25; ++i) {
    if (num1[i] != num2[i]) {
      cout << -1 << '\n';
      return;
    }
  }
  long long maxx = 0;
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 0; j <= 25; ++j) last[j] = 0;
    for (long long j = 1; j <= n; ++j) {
      if (t[i] == s[j]) {
        dp[i][j] =
            (i == 1) ? 1 : max(dp[i][j], dp[i - 1][last[t[i - 1] - 'a']] + 1);
      }
      last[s[j] - 'a'] = j;
      maxx = max(dp[i][j], maxx);
    }
  }
  cout << n - maxx << '\n';
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> T;
  while (T--) solve();
  return 0;
}
