#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\n"; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    string A, B;
    cin >> A >> B;
    bool flg = false;
    for (int i = (0), i_end_ = (n); i < i_end_; i++)
      if (A[i] > B[i]) {
        flg = true;
        break;
      }
    if (flg) {
      cout << -1 << "\n";
      continue;
    }
    int ans = 0;
    for (int i = (0), i_end_ = (20); i < i_end_; i++) {
      char minchar = 'z';
      for (int j = (0), j_end_ = (n); j < j_end_; j++)
        if (A[j] == ('a' + i) && A[j] < B[j]) minchar = min(minchar, B[j]);
      if (minchar != 'z') {
        ans++;
        for (int j = (0), j_end_ = (n); j < j_end_; j++)
          if (A[j] == ('a' + i) && A[j] < B[j]) A[j] = minchar;
      }
    }
    cout << ans << "\n";
  }
}
