#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
map<pair<char, char>, int> vis;
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    vis.clear();
    string A, B;
    int n;
    cin >> n;
    cin >> A >> B;
    int res = 0;
    for (int i = 0; i < A.length(); ++i) {
      if (B[i] < A[i]) {
        res = -INF;
        break;
      }
      if (B[i] != A[i]) {
        vis[make_pair(A[i], B[i])] = 1;
      }
    }
    int las = -1, mo = -1;
    for (map<pair<char, char>, int>::iterator ite = vis.begin();
         ite != vis.end(); ite++) {
      if (ite->first.first != las) {
        res++;
        las = ite->first.first;
        mo = ite->first.second;
      } else {
        vis[make_pair(mo, ite->first.second)] = 1;
      }
    }
    cout << max(res, -1) << endl;
  }
  return 0;
}
