#include <bits/stdc++.h>
using namespace std;
int z[101010][21];
int main() {
  int t;
  cin >> t;
  for (int x = 0; x < t; x++) {
    int n;
    string a, b;
    cin >> n;
    cin >> a >> b;
    bool bo = true;
    int yr = 0;
    for (char s = 'a'; s <= 't'; s++) {
      char mi = 'z';
      for (int i = 0; i < n; i++) {
        if (a[i] == s && b[i] != s) {
          mi = min(mi, b[i]);
        }
      }
      if (mi == 'z') continue;
      if (mi < s) bo = false;
      if (a == b) break;
      yr++;
      for (int i = 0; i < n; i++) {
        if (a[i] != b[i] && a[i] == s) {
          a[i] = mi;
        }
      }
    }
    if (bo) {
      cout << yr << "\n";
    } else
      cout << -1 << "\n";
  }
}
