#include <bits/stdc++.h>
using namespace std;
void solve() {
  int N;
  cin >> N;
  string s, t;
  cin >> s >> t;
  vector<int> freq(26, 0);
  for (int i = 0; i < N; ++i) {
    freq[s[i] - 'a']++;
    freq[t[i] - 'a']--;
  }
  for (int i = 0; i < 26; ++i) {
    if (freq[i] != 0) {
      cout << "-1\n";
      return;
    }
  }
  int best = 0;
  for (int l = 0; l < N; ++l) {
    int ptr = 0;
    bool fail = false;
    for (int r = l; r < N && !fail; ++r) {
      while (ptr < N && s[ptr] != t[r]) {
        ptr++;
      }
      if (ptr >= N) fail = true;
      if (!fail) {
        best = max(best, r - l + 1);
        ptr++;
      }
    }
  }
  cout << (N - best) << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int Q;
  cin >> Q;
  for (int q = 0; q < Q; ++q) solve();
  return 0;
}
