#include <bits/stdc++.h>
using namespace std;
int f[30], ans, t, n;
string st1, st2;
int fafa(int x) {
  if (f[x] == x) return x;
  return f[x] = fafa(f[x]);
}
void hebing(int x, int y) {
  x = fafa(x);
  y = fafa(y);
  if (x != y) {
    ans++;
    f[x] = y;
  }
}
int main() {
  cin >> t;
  for (int tt = 1; tt <= t; tt++) {
    cin >> n;
    for (int i = 1; i <= 26; i++) f[i] = i;
    cin >> st1;
    cin >> st2;
    bool sit = true;
    ans = 0;
    for (int i = 0; i < n; i++) {
      if (st1[i] > st2[i]) {
        cout << "-1\n";
        sit = false;
        break;
      }
      hebing(st1[i] - 'a' + 1, st2[i] - 'a' + 1);
    }
    if (sit) cout << ans << endl;
    ans = 0;
  }
  return 0;
}
