#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int q, n;
  cin >> q;
  while (q--) {
    cin >> n;
    string stra, strb;
    cin >> stra >> strb;
    int ans = (0x3f3f3f3f);
    int cur;
    for (int i = 0; i != n; ++i) {
      cur = i;
      for (int j = 0; j != n; ++j) {
        if (cur != n && strb[cur] == stra[j]) ++cur;
      }
      ans = min(ans, n - cur + i);
    }
    sort(stra.begin(), stra.end());
    sort(strb.begin(), strb.end());
    if (stra != strb) {
      cout << -1 << '\n';
      continue;
    }
    cout << ans << '\n';
  }
}
