#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, ans = 1e18;
    string s, t;
    cin >> n >> s >> t;
    long long cnt[26] = {};
    for (long long i = 0; i < n; i++) cnt[s[i] - 'a']++;
    for (long long i = 0; i < n; i++) cnt[t[i] - 'a']--;
    for (long long i = 0; i < 26; i++)
      if (cnt[i]) ans = -1;
    for (long long i = 0; i < n; i++) {
      long long ptr = i, cnt = 0;
      for (long long j = 0; j < n; j++)
        if (ptr < n && s[j] == t[ptr]) cnt++, ptr++;
      ans = min(ans, n - cnt);
    }
    cout << ans << endl;
  }
}
