#include <bits/stdc++.h>
using namespace std;
void marmot0814() {
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  set<pair<char, char>> st;
  for (int i = 0; i < n; i++) {
    if (s[i] > t[i]) {
      cout << -1 << '\n';
      return;
    }
  }
  int ans = 0;
  while (s != t) {
    char minV = 'z';
    for (int i = 0; i < n; i++) {
      if (s[i] == t[i]) continue;
      minV = min(minV, s[i]);
    }
    vector<int> target;
    char tminV = 'z';
    for (int i = 0; i < n; i++) {
      if (s[i] == t[i]) continue;
      if (s[i] == minV) {
        target.push_back(i);
        tminV = min(tminV, t[i]);
      }
    }
    ans++;
    for (auto &v : target) {
      s[v] = tminV;
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) marmot0814();
}
