#include <bits/stdc++.h>
using namespace std;
int fa[2000];
int get(int x) {
  if (x == fa[x]) return x;
  return fa[x] = get(fa[x]);
}
int merge(int x, int y) { fa[get(x)] = fa[get(y)]; }
bool cmp(pair<int, int> a, pair<int, int> b) {
  return a.second - a.first < b.second - b.first;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    int ans = 0;
    int len = s.length();
    for (int i = 'a'; i < 'z'; i++) fa[i] = i;
    for (int i = 0; i < len; i++)
      if (s[i] > t[i]) ans = -1;
    if (ans == -1)
      cout << ans << endl;
    else {
      vector<pair<int, int> > temp;
      for (int i = 0; i < len; i++)
        if (s[i] != t[i]) temp.push_back(make_pair(s[i], t[i]));
      sort(temp.begin(), temp.end(), cmp);
      for (pair<int, int> it : temp) {
        if (get(it.first) != get(it.second)) {
          ans++;
          merge(it.first, it.second);
        }
      }
      cout << ans << endl;
    }
  }
}
