#include <bits/stdc++.h>
using namespace std;
inline long long gcd(long long a, long long b) {
  return b > 0 ? gcd(b, a % b) : a;
}
inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
inline long long qpow(long long a, long long n) {
  long long ans = 1;
  while (n) {
    if (n & 1) {
      ans *= a;
    }
    a *= a;
    n >>= 1;
  }
  return ans;
}
inline long long _qpow(long long a, long long n) {
  long long ans = 1;
  while (n) {
    if (n & 1) {
      ans *= a % 1000000007;
      ans %= 1000000007;
    }
    a *= a;
    a %= 1000000007;
    n >>= 1;
  }
  return ans % 1000000007;
}
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') {
      f = -1;
    }
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const long long maxnum = 1e6 + 5;
long long a[maxnum];
signed main() {
  long long T = read();
  while (T--) {
    long long n = read();
    string a, b;
    cin >> a >> b;
    bool flag = false;
    for (long long i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        flag = true;
      }
    }
    if (flag) {
      cout << "-1" << endl;
      continue;
    }
    long long ans = 0;
    for (long long c = 'a'; c <= 'z'; c++) {
      long long MIN = 0x3f3f3f3f3f3f3f3f;
      flag = false;
      for (long long i = 0; i < n; i++) {
        if (a[i] == c && a[i] != b[i]) {
          MIN = min(b[i] + 0ll, MIN);
          flag = true;
        }
      }
      if (flag) {
        for (long long i = 0; i < n; i++) {
          if (a[i] == c && a[i] != b[i]) {
            a[i] = MIN;
          }
        }
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
