#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  unsigned long long t;
  cin >> t;
  while (t--) {
    unsigned long long n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    set<unsigned long long> v[20];
    unsigned long long f = 0;
    for (unsigned long long i = 0; i < n; i++) {
      if (a[i] == b[i]) {
        continue;
      } else if (a[i] > b[i]) {
        f = 1;
        break;
      } else {
        unsigned long long x = a[i] - 'a';
        unsigned long long y = b[i] - 'a';
        v[x].insert(y);
        v[y].insert(x);
      }
    }
    if (f == 1) {
      cout << -1 << endl;
    } else {
      vector<unsigned long long> vis(20, 0);
      unsigned long long ans = 0;
      for (unsigned long long i = 0; i < 20; i++) {
        unsigned long long c = 0;
        if (vis[i] == 0 && v[i].size() > 0) {
          queue<unsigned long long> q;
          q.push(i);
          vis[i] = 1;
          while (!q.empty()) {
            unsigned long long ff = q.front();
            q.pop();
            c++;
            for (auto u : v[ff]) {
              if (vis[u] == 0) {
                q.push(u);
                vis[u] = 1;
              }
            }
          }
          ans += c - 1;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}
