#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long INF = 4e18 + 10;
const long long INF2 = 2e18 + 10;
const long long INF3 = 2e10;
const long long MAXN = 70000;
const long long LOGN = 19;
const long long MOD = 1000000007;
const long double EPS = 1e-7;
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n), b(n);
  {
    string s;
    cin >> s;
    for (long long i = 0; i < n; i++) {
      a[i] = (long long)s[i] - 97;
    }
    cin >> s;
    for (long long i = 0; i < n; i++) {
      b[i] = (long long)s[i] - 97;
    }
  }
  for (long long i = 0; i < n; i++) {
    if (b[i] < a[i]) {
      cout << "-1\n";
      return;
    }
  }
  set<long long> s;
  long long ans = 0;
  for (long long k = 0; k < 20; k++) {
    s.clear();
    for (long long i = 0; i < n; i++) {
      if (b[i] == k && a[i] != b[i]) {
        s.insert(a[i]);
      }
    }
    for (long long i = 0; i < n; i++) {
      if (s.count(a[i]) && a[i] != b[i]) a[i] = k;
    }
    ans += s.size();
  }
  cout << ans << '\n';
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long T;
  cin >> T;
  while (T--) solve();
}
