#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int N = 1e5 + 5;
void solve() {
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  string a = s, b = t;
  sort(begin(a), end(a));
  sort(begin(b), end(b));
  if (a != b) {
    cout << "-1\n";
    return;
  }
  int mx = 0;
  for (int i = 0; i < n; i++) {
    int j = i;
    for (char &c : s) {
      if (j < n && t[j] == c) j++;
    }
    mx = max(mx, j - i);
  }
  cout << n - mx << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) solve();
}
