#include <bits/stdc++.h>
using namespace std;
char ft(char c) { return c; }
int main() {
  int test = 1;
  cin >> test;
  while (test--) {
    int i, j, m;
    cin >> m;
    string a1, a2;
    cin >> a1 >> a2;
    map<char, set<char> > mp;
    map<char, set<char> >::iterator itr;
    set<char>::iterator itr2;
    for (i = 0; i < m; i++) {
      if (a1[i] == a2[i]) continue;
      if (a1[i] > a2[i])
        break;
      else
        mp[a1[i]].insert(a2[i]);
    }
    int aa = 0;
    if (i < m)
      cout << "-1";
    else {
      for (itr = mp.begin(); itr != mp.end(); itr++) {
        if ((itr->second).size() == 0) continue;
        itr2 = (itr->second).begin();
        char c = (*itr2);
        itr2++;
        for (; itr2 != (itr->second).end(); itr2++) mp[c].insert(*itr2);
        aa++;
      }
      cout << aa;
    }
    cout << "\n";
  }
  return 0;
}
