#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int mod0 = 1e9 + 7;
const long long mod1 = 998244353;
const long long mod2 = 1e9 + 9;
const long long mod3 = 2147483647;
const long long inf = 2 * 1024 * 1024 * 1023 - 1;
const long long INF = inf * inf;
const int sz = 256 * 1024;
const long double eps = 1e-7;
void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(16);
}
void solve() {
  int n;
  string s, t;
  cin >> n >> s >> t;
  vector<int> cnt(26);
  for (int i = 0; i < n; ++i) {
    cnt[s[i] - 'a']++;
    cnt[t[i] - 'a']--;
  }
  if (*min_element((cnt).begin(), (cnt).end()) != 0 ||
      *max_element((cnt).begin(), (cnt).end()) != 0) {
    cout << -1 << '\n';
    return;
  }
  int ans = 500;
  for (int l = 0; l < n + 1; ++l) {
    for (int r = l; r <= n; ++r) {
      int len = r - l + 1;
      int x = n - len;
      int i = l;
      for (int j = 0; j < n; ++j) {
        if (s[j] == t[i]) i++;
        if (i > r) break;
      }
      if (i > r) ans = min(ans, x);
    }
  }
  cout << ans << '\n';
}
bool multitest = true;
int main() {
  init();
  int t = 1;
  if (multitest) cin >> t;
  for (int i = 0; i < t; ++i) solve();
  return 0;
}
