#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long int mod = 1000000007;
const long long int N = 300001;
const long long int M = log2(N);
struct point {
  long long int x, y;
};
struct node {
  long long int t, p, d, i;
};
void debug(stringstream &s) {}
template <typename T, typename... Args>
void debug(stringstream &s, T a, Args... args) {
  string word;
  s >> word;
  cerr << word << " = " << a << endl;
  debug(s, args...);
}
long long int fast_exp(long long int x, long long int n) {
  if (n == 0) return 1;
  if (n & 1)
    return fast_exp(x, n - 1) * x % mod;
  else {
    long long int ans = fast_exp(x, n / 2);
    return ans * ans % mod;
  }
}
long long int dp[101][2001];
bool cmp(node a, node b) { return a.d < b.d; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int _ = 1;
  cin >> _;
  int test = 0;
  while (_--) {
    test++;
    long long int n, m, x = 0, y = 0, k, ans = 1;
    cin >> n;
    string a, b;
    cin >> a >> b;
    long long int cnt[20][20] = {};
    for (int i = 0; i < n; i++) {
      if (a[i] != b[i]) cnt[a[i] - 'a'][b[i] - 'a'] = 1;
      if (a[i] > b[i]) ans = 0;
    }
    if (ans) {
      ans = 0;
      for (int i = 0; i < 20; i++) {
        vector<int> v;
        for (int j = i + 1; j < 20; j++) {
          if (cnt[i][j]) v.push_back(j);
        }
        if (v.size() > 0) {
          ans++;
          for (int j = 1; j < v.size(); j++) {
            cnt[v[0]][v[j]] = 1;
          }
        }
      }
      cout << ans << "\n";
    } else
      cout << "-1\n";
  }
  return 0;
}
