#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  char d = '1';
  vector<set<char>> temp(20);
  set<char> r;
  for (int i = 0; i < n; ++i) {
    if (a[i] > b[i]) {
      cout << -1 << endl;
      return;
    } else if (a[i] != b[i]) {
      temp[a[i] - 'a'].insert(b[i]);
    }
  }
  int ans = 0;
  bool flag;
  for (char d = 'a'; d <= 't'; d++) {
    flag = false;
    char s = 'z';
    for (int i = 0; i < n; ++i) {
      if (a[i] == d && b[i] != d) {
        flag = true;
        s = min(s, b[i]);
      }
    }
    if (flag) ans++;
    for (int i = 0; i < n; ++i) {
      if (a[i] == d && b[i] != d) {
        a[i] = s;
      }
    }
  }
  cout << ans << endl;
}
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
