#include <bits/stdc++.h>
const double pi = acos(-1);
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b != 0) {
    if (b & 1) res *= a;
    a *= a;
    b >>= 1;
  }
  return res;
}
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    int n;
    bool flag = 1;
    cin >> n;
    string a, b;
    cin >> a >> b;
    for (int i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        flag = 0;
        break;
      }
    }
    if (flag) {
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        int cnt = 0;
        char c = 'z';
        for (int j = 0; j < n; j++) {
          if (a[j] == ('a' + i) && b[j] != a[j]) cnt++, c = min(c, b[j]);
        }
        if (cnt) {
          ans++;
          for (int j = 0; j < n; j++) {
            if (a[j] == ('a' + i) && b[j] != a[j]) a[j] = c;
          }
        }
      }
      cout << ans << "\n";
    } else
      cout << "-1\n";
  }
}
