#include <bits/stdc++.h>
using namespace std;
map<pair<long long, long long>, long long> M;
long long A[200005];
long long i, n;
string S, T;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n;
    cin >> S;
    cin >> T;
    for (i = 0; i < n; i++) {
      if (S[i] > T[i]) {
        break;
      } else if (S[i] < T[i]) {
        M[{S[i] - 'a', T[i] - 'a'}]++;
      }
    }
    if (i >= n) {
      long long sm = 0;
      for (long long j = 0; j < 20; j++) {
        long long c = 0, cn;
        for (long long k = j + 1; k < 20; k++) {
          if (!c && M[{j, k}]) {
            c = 1;
            cn = k;
          } else if (c && M[{j, k}]) {
            M[{cn, k}] += M[{j, k}];
            M[{j, k}] = 0;
          }
        }
        if (c) sm++;
      }
      cout << sm << endl;
    } else
      cout << -1 << endl;
    M.clear();
  }
  return 0;
}
