#include <bits/stdc++.h>
using namespace std;
void setup() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  srand(time(0));
}
const int C = 27;
void solve() {
  int n;
  string s, t;
  cin >> n >> s >> t;
  if (s == t) {
    cout << 0 << '\n';
    return;
  }
  {
    string z = s, p = t;
    sort(z.begin(), z.end());
    sort(p.begin(), p.end());
    if (p != z) {
      cout << -1 << '\n';
      return;
    }
  }
  int ans = 1e9;
  for (int i = -1; i < n; ++i) {
    for (int j = n; j >= i + 2; --j) {
      int uk = i + 1;
      for (auto k : s) {
        if (uk == j) break;
        if (t[uk] == k) ++uk;
      }
      if (uk == j) ans = min(ans, i + 1 + (n - j));
    }
  }
  cout << ans << '\n';
}
signed main() {
  setup();
  int t;
  cin >> t;
  while (t--) solve();
  ;
}
