#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    vector<int> cnt1(26, 0), cnt2(26, 0);
    for (char c : s) cnt1[c - 'a']++;
    for (char c : t) cnt2[c - 'a']++;
    bool did = 0;
    for (int i = 0; i < 26; i++)
      if (cnt1[i] != cnt2[i]) {
        cout << -1 << '\n';
        did = 1;
        break;
      }
    if (did) continue;
    int ans = -1;
    int tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp = 0;
      for (int j = 0; j < n && i + tmp < n; j++) {
        if (s[j] == t[i + tmp]) tmp++, ans = max(ans, tmp);
      }
    }
    cout << (ans == -1 ? -1 : n - ans) << '\n';
  }
}
