#include <bits/stdc++.h>
static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;
template <class T>
constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;
void solve() {
  int n;
  string s, t;
  cin >> n >> s >> t;
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int cur = i;
    for (int j = 0; j < n; ++j) {
      if (cur < n && t[cur] == s[j]) cur++;
    }
    ans = max(ans, cur - i);
  }
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());
  if (s != t)
    puts("-1");
  else
    cout << n - ans << "\n";
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    solve();
  }
  return 0;
}
