#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
  long long n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  for (long long i = 0; i < n; i++) {
    if (a[i] > b[i]) {
      cout << "-1\n";
      return;
    }
  }
  vector<long long> need(20);
  for (long long i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      long long v = a[i] - 'a';
      long long to = b[i] - 'a';
      need[v] |= 1 << to;
    }
  }
  long long ans = 0;
  auto last_bit = [&](long long x) { return x & -x; };
  for (long long i = 0; i < 20; i++) {
    if (need[i] == 0) continue;
    long long x = 0;
    while (!((need[i] >> x) & 1)) x++;
    need[i] ^= 1 << x;
    need[x] |= need[i];
    ans++;
  }
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long q;
  cin >> q;
  while (q--) {
    solve();
  }
}
