#include <bits/stdc++.h>
using namespace std;
template <class A1>
void prn(A1&& arg) {
  cout << arg << '\n';
}
template <class A1, class... A>
void prn(A1&& arg, A&&... args) {
  cout << arg << ' ';
  prn(args...);
}
void dfs(long long x, vector<long long> v[], bool b[], long long pa = 0) {
  b[x] = 1;
  for (long long y : v[x]) {
    if (y == pa) continue;
    if (!b[y]) dfs(y, v, b, x);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    set<char> s;
    string a, b;
    cin >> a >> b;
    for (char c : a) s.insert(c);
    for (char c : b) s.insert(c);
    bool fl = 1;
    for (long long i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        fl = 0;
        break;
      }
    }
    if (!fl) {
      prn(-1);
      continue;
    }
    vector<long long> v[25];
    for (long long i = 0; i < n; i++) {
      v[a[i] - 'a' + 1].emplace_back(b[i] - 'a' + 1);
      v[b[i] - 'a' + 1].emplace_back(a[i] - 'a' + 1);
    }
    bool vis[25] = {0};
    long long ans = s.size();
    for (char c : s) {
      if (!vis[c - 'a' + 1]) {
        dfs(c - 'a' + 1, v, vis);
        ans--;
      }
    }
    prn(ans);
  }
}
