#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
void solve() {
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  vector<int> cnt(26);
  for (int i = 0; i < n; ++i) {
    cnt[s[i] - 'a']++;
    cnt[t[i] - 'a']--;
  }
  for (int i = 0; i < 26; ++i) {
    if (cnt[i] != 0) {
      cout << -1 << "\n";
      return;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int ptr1 = i;
    int ptr2 = 0;
    int val = 0;
    while (ptr2 < n && ptr1 < n) {
      if (t[ptr1] == s[ptr2]) {
        ++val;
        ++ptr1;
      }
      ++ptr2;
    }
    ans = max(ans, val);
  }
  cout << n - ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
