#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
void f() {
  int n;
  string s, t;
  cin >> n >> s >> t;
  map<char, int> d1, d2;
  for (int i = 0; i < n; i++) {
    ++d1[s[i]];
    ++d2[t[i]];
  }
  if (d1 != d2) {
    cout << -1;
    return;
  }
  int ans = 0;
  int l = 1, r = n, mid;
  while (l <= r) {
    mid = (l + r) / 2;
    bool cs = false;
    for (int left = 0; left + mid - 1 < n; left++) {
      int ptr = left;
      for (int i = 0; i < n && ptr < left + mid; i++) {
        if (s[i] == t[ptr]) {
          ++ptr;
        }
      }
      if (ptr == left + mid) {
        cs = true;
        break;
      }
    }
    if (cs) {
      ans = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  cout << n - ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q;
  cin >> q;
  while (q--) {
    f();
    cout << '\n';
  }
}
