#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, b % a);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    long long int n, i, p = 0;
    string s1, s2;
    char j, k;
    cin >> n >> s1 >> s2;
    map<char, map<char, int>> mp;
    for (i = 0; i < n; i++) {
      if (s1[i] > s2[i]) {
        p++;
        break;
      } else if (s1[i] < s2[i]) {
        mp[s1[i]][s2[i]]++;
      }
    }
    if (p) {
      cout << -1 << endl;
      continue;
    }
    for (k = 'a'; k <= 'z'; k++) {
      for (j = k + 1; j <= 'z'; j++) {
        if (mp[k][j]) {
          for (i = j + 1; i <= 'z'; i++) {
            if (mp[k][i]) {
              mp[j][i]++;
              mp[k][i] = 0;
            }
          }
        }
      }
    }
    for (k = 'a'; k <= 'z'; k++) {
      for (j = k + 1; j <= 'z'; j++) {
        {
          if (mp[k][j]) {
            p++;
          }
        }
      }
    }
    cout << p << endl;
  }
  return 0;
}
