#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int l;
    cin >> l;
    string s, t;
    cin >> s >> t;
    int cnts[26], cntt[26];
    for (int i = 0; i < 26; i++) {
      cnts[i] = cntt[i] = 0;
    }
    for (int i = 0; i < l; i++) {
      cnts[s[i] - 'a']++;
      cntt[t[i] - 'a']++;
    }
    bool con = true;
    for (int i = 0; i < 26; i++) {
      if (cnts[i] != cntt[i]) {
        con = false;
        break;
      }
    }
    if (!con) {
      cout << -1 << endl;
      continue;
    }
    int mx = 0;
    for (int st = 0; st < l; st++) {
      int i = st;
      int j = 0;
      while (i < l) {
        while (j < l && s[j] != t[i]) {
          j++;
        }
        if (j >= l) {
          break;
        } else {
          i++;
          j++;
        }
      }
      mx = max(mx, i - st);
    }
    cout << l - mx << endl;
  }
}
