#include <bits/stdc++.h>
int main() {
  int q;
  std::cin >> q;
  for (int i = 0; i < q; ++i) {
    int n;
    std::cin >> n;
    int ans = n;
    std::string s1, s2, ss1, ss2;
    std::cin >> s1 >> s2;
    ss1 = s1;
    ss2 = s2;
    sort(ss1.begin(), ss1.end());
    sort(ss2.begin(), ss2.end());
    if (ss1 != ss2) {
      ans = -1;
    } else {
      for (int i = 0; i < n; i++) {
        int maxpref = 0;
        for (int k = 0; k < n; k++) {
          if ((i + maxpref < n) && (s1[k] == s2[i + maxpref])) {
            ++maxpref;
          }
        }
        ans = std::min(ans, n - maxpref);
      }
    }
    std::cout << ans << std::endl;
  }
}
