#include <bits/stdc++.h>
using namespace std;
int par[21], siz[21];
int root(int a) {
  while (a != par[a]) {
    a = par[a];
  }
  return a;
}
void unionn(int a, int b) {
  int root_a = root(a);
  int root_b = root(b);
  if (root_a == root_b) {
    return;
  }
  if (siz[root_a] < siz[root_b]) {
    siz[root_b] += siz[root_a];
    par[root_a] = root_b;
  } else {
    siz[root_a] += siz[root_b];
    par[root_b] = root_a;
  }
}
void init() {
  int i;
  for (i = 0; i <= 20; i++) {
    siz[i] = 1;
    par[i] = i;
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    init();
    int n;
    cin >> n;
    int i;
    string s1, s2;
    cin >> s1 >> s2;
    s1 = '@' + s1;
    s2 = '@' + s2;
    int flag = 0;
    for (i = 1; i <= n; i++) {
      if (s1[i] > s2[i]) {
        flag = 1;
      }
    }
    if (flag == 1) {
      cout << "-1\n";
      continue;
    }
    set<int> v[20];
    int ans = 0;
    for (i = 1; i <= n; i++) {
      if (s1[i] != s2[i]) v[s2[i] - 'a'].insert(s1[i] - 'a');
    }
    map<pair<int, int>, int> m;
    set<int> sp;
    for (i = 1; i <= 19; i++) {
      if (v[i].size() == 0) {
        continue;
      }
      set<int> sp;
      for (auto it = v[i].begin(); it != v[i].end(); it++) {
        sp.insert(root(*it));
        unionn(*it, i);
      }
      ans += (int)(sp.size());
    }
    cout << ans << endl;
  }
}
