#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int q, k;
  cin >> q;
  for (k = 0; k < q; k++) {
    int n;
    cin >> n;
    string s, t, st, tt;
    cin >> s >> t;
    st = s;
    tt = t;
    sort(st.begin(), st.end());
    sort(tt.begin(), tt.end());
    if (st != tt) {
      cout << -1 << endl;
    } else {
      int mA = 0;
      for (int i = 0; i < n; i++) {
        int po = i;
        int j = 0;
        while (j < n && po < n) {
          if (s[j] == t[po]) po++;
          ++j;
        }
        mA = max(mA, po - i);
      }
      cout << n - mA << endl;
    }
  }
}
