#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
int k[33][33];
int main() {
  int q, n;
  string a, b;
  cin >> q;
  while (q--) {
    cin >> n >> a >> b;
    int f = 1;
    memset(k, 0, sizeof k);
    for (int i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        f = 0;
        break;
      }
      if (a[i] == b[i]) continue;
      k[a[i] - 'a'][b[i] - 'a']++;
    }
    if (f == 0) {
      cout << -1 << '\n';
      continue;
    }
    int res = 0, ok = -1;
    for (int i = 0; i < 20; i++) {
      ok = -1;
      for (int j = i + 1; j < 20; j++) {
        if (k[i][j] > 0) {
          ok = j;
          break;
        }
      }
      if (ok == -1) {
        continue;
      }
      for (int j = i + 1; j < 20; j++) {
        if (k[i][j] > 0) {
          if (ok == j) {
            continue;
          }
          k[ok][j] += k[i][j];
        }
      }
      res++;
    }
    cout << res << '\n';
  }
  return 0;
}
