#include <bits/stdc++.h>
using namespace std;
int T, N;
string S1, S2;
bool chk() {
  int cnt[2][26] = {};
  for (int i = 0; i <= N - 1; i++) {
    cnt[0][(int)(S1[i] - 'a')]++;
    cnt[1][(int)(S2[i] - 'a')]++;
  }
  for (int i = 0; i <= 25; i++)
    if (cnt[0][i] != cnt[1][i]) return 0;
  return 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> T;
  while (T--) {
    cin >> N >> S1 >> S2;
    if (chk() == 0) {
      cout << -1 << '\n';
      continue;
    }
    int mx = 0;
    for (int i = 1; i <= N; i++) {
      int now = i;
      for (int j = 1; j <= N; j++) {
        if (S2[now - 1] == S1[j - 1]) now++;
        if (now == N + 1) break;
      }
      mx = max(mx, now - i);
    }
    cout << N - mx << '\n';
  }
  return 0;
}
