#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a < b) swap(a, b);
  if (b == 0) return a;
  while ((a = a % b) != 0) {
    swap(a, b);
  }
  return b;
}
long double ncr(int n, int r) {
  long double ans = 1;
  int c = 0;
  for (int i = 1; i < r + 1; i++) {
    ans = ans * (n - r + i) / i;
    while (ans > 1 && c < n) {
      ans *= 0.5;
      c++;
    }
  }
  while (c < n) {
    ans *= 0.5;
    c++;
  }
  return ans;
}
void solve() {
  int l;
  string s, t;
  cin >> l >> s >> t;
  int cnt[26] = {0};
  vector<int> ind[26];
  for (int i = 0; i < l; i++) {
    cnt[(int)t[i] - 97]++;
    ind[(int)s[i] - 97].push_back(i);
  }
  for (int i = 0; i < 26; i++) {
    if (cnt[i] != ind[i].size()) {
      cout << -1;
      return;
    }
  }
  int x, mx = 1, p;
  for (int i = 0; i < l; i++) {
    x = 1;
    p = ind[(int)t[i] - 97][0];
    for (int j = i + 1; j < l; j++) {
      if (lower_bound(ind[(int)t[j] - 97].begin(), ind[(int)t[j] - 97].end(),
                      p + 1) == ind[(int)t[j] - 97].end())
        break;
      p = *lower_bound(ind[(int)t[j] - 97].begin(), ind[(int)t[j] - 97].end(),
                       p + 1);
      x++;
    }
    mx = max(mx, x);
  }
  cout << l - mx;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
    cout << "\n";
  }
}
