#include <bits/stdc++.h>
using namespace std;
template <typename Tp>
inline void outarr(Tp _begin, Tp _end, const char* _delim = " ") {
  for (Tp current = _begin; current != _end; ++current) {
    std::cout << *current << _delim;
  }
  std::cout << '\n';
}
using ll = long long;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);
vector<vector<int>> Parse(string_view s) {
  vector<vector<int>> suff(s.size() + 1, vector<int>(26, 0));
  for (int i = static_cast<int>((s).size()) - 1; i >= 0; --i) {
    suff[i] = suff[i + 1];
    ++suff[i][s[i] - 'a'];
  }
  return suff;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int test_cases;
  cin >> test_cases;
  string s, t;
  while (test_cases--) {
    int n;
    cin >> n >> t >> s;
    auto suff_s = Parse(s);
    auto suff_t = Parse(t);
    if (suff_s[0] != suff_t[0]) {
      cout << -1 << "\n";
      continue;
    }
    vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= i; ++j) {
        dp[i][j] = dp[i][j - 1];
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
        } else if (suff_t[j][s[i - 1] - 'a'] > suff_s[i][s[i - 1] - 'a']) {
          dp[i][j] = max(dp[i][j], dp[i - 1][j]);
        }
      }
    }
    cout << n - dp[n][n] << "\n";
  }
  return 0;
}
