#include <bits/stdc++.h>
using namespace std;
void func() {
  long long n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  set<long long> s[26];
  for (long long i = 0; i < n; i++) {
    if (a[i] > b[i]) {
      cout << "-1\n";
      return;
    }
    if (a[i] != b[i]) {
      s[a[i] - 'a'].insert(b[i] - 'a');
    }
  }
  long long ans = 0;
  for (long long i = 0; i < 26; i++) {
    if ((long long)s[i].size() != 0) {
      if ((long long)s[i].size() == 1)
        ans++;
      else {
        long long x = (*s[i].begin());
        s[i].erase(s[i].begin());
        for (auto j : s[i]) s[x].insert(j);
        ans++;
      }
    }
  }
  cout << ans << "\n";
}
int main() {
  {
    ios ::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
  long long ntc = 1;
  cin >> ntc;
  for (long long i = 1; i < ntc + 1; i++) {
    func();
  }
  return 0;
}
