#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int M = 1e3 + 5;
const long long mod = 1e9 + 7;
int n;
string a, b;
void main_solve() {
  cin >> n >> a >> b;
  string aa = a, bb = b;
  sort(aa.begin(), aa.end());
  sort(bb.begin(), bb.end());
  if (aa != bb) {
    puts("-1");
    return;
  }
  int ans = 0x3f3f3f3f;
  for (int i = 0; i <= n - 1; ++i) {
    int k = i;
    for (int j = 0; j <= n - 1; ++j) {
      if (k < n and a[j] == b[k]) ++k;
    }
    ans = min(ans, n - k + i);
  }
  printf("%d\n", ans);
  return;
}
signed main() {
  int T = 1;
  cin >> T;
  while (T--) {
    main_solve();
  }
  return 0;
}
