#include <bits/stdc++.h>
using namespace std;
void flash() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
const long long N = 30;
long long f[N + 10];
void init() {
  for (long long i = 0; i < N; i++) f[i] = i;
}
long long fnd(long long x) { return f[x] == x ? x : f[x] = fnd(f[x]); }
void solve() {
  long long n;
  cin >> n;
  init();
  string a, b;
  cin >> a >> b;
  map<long long, set<long long>> mp;
  for (long long i = 0; i < n; i++) {
    if (a[i] > b[i]) {
      cout << "-1"
           << "\n";
      return;
    }
    if (a[i] != b[i]) {
      mp[b[i] - 'a'].insert(a[i] - 'a');
    }
  }
  long long ans = 0;
  for (long long i = 0; i < 27; i++) {
    if ((long long)mp[i].size() == 0) {
      continue;
    }
    set<long long> tmp;
    for (auto it : mp[i]) {
      tmp.insert(fnd(it));
    }
    ans += (long long)tmp.size();
    for (auto it : mp[i]) {
      f[fnd(it)] = f[fnd(i)];
    }
  }
  cout << ans << "\n";
}
int32_t main() {
  flash();
  long long t;
  t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
