#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int test = 1;
  cin >> test;
  for (; test > 0; --test) {
    int n, ch = 0;
    cin >> n;
    string s, s1;
    set<pair<char, char>> se;
    cin >> s >> s1;
    for (int i = 0; i < n; ++i) {
      if (s[i] != s1[i]) {
        if (s[i] > s1[i]) {
          ch = 1;
          break;
        } else
          se.insert(make_pair(s[i], s1[i]));
      }
    }
    if (ch) {
      cout << "-1" << endl;
      continue;
    }
    int c = 0;
    while (se.size() != 0) {
      pair<long long int, long long int> f = (*se.begin());
      c++;
      se.erase(f);
      while (se.size()) {
        pair<long long int, long long int> f1 = (*se.begin());
        if (f1.first != f.first) break;
        se.erase(f1);
        if (f1.second != f.second) se.insert(make_pair(f.second, f1.second));
      }
    }
    cout << c << endl;
  }
  return 0;
}
