#include <bits/stdc++.h>
using namespace std;
bool sortinrev(const pair<long long int, long long int> &a,
               const pair<long long int, long long int> &b) {
  if (a.first != b.first)
    return (a.first > b.first);
  else
    return (a.second < b.second);
}
long long int power(long long int x, long long int n, long long int m) {
  long long int result = 1;
  while (n > 0) {
    if (n % 2 == 1) result = (result * x) % m;
    x = (x * x) % m;
    n = n / 2;
  }
  return result % m;
}
bool isPrime(long long int k) {
  if (k <= 1) {
    return false;
  }
  if (k == 2) {
    return true;
  }
  if (k % 2 == 0) {
    return false;
  }
  long long int i = 3;
  while (i * i <= k) {
    if (k % i == 0) {
      return false;
    }
    i += 2;
  }
  return true;
}
long long int bin(long long int n) {
  int di = 0;
  while (n) {
    di = di + n % 2;
    n = n / 2;
  }
  return di;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  t = 1;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    long long int ans = 0;
    for (long long int i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        ans = -1;
        break;
      }
    }
    if (ans == -1) {
      cout << "-1\n";
      continue;
    } else {
      long long int ans = 0;
      map<char, vector<long long int>> m;
      for (long long int i = 0; i < n; i++) m[a[i]].push_back(i);
      for (char i = 'a'; i < 'u'; i++) {
        char z = 'z';
        vector<long long int> x;
        for (long long int j = 0; j < m[i].size(); j++) {
          long long int g = m[i][j];
          if (a[g] != b[g]) {
            z = min(b[g], z);
            x.push_back(g);
          }
        }
        if (x.size() == 0)
          continue;
        else {
          ans++;
          for (long long int j = 0; j < x.size(); j++) {
            char v = z;
            a[x[j]] = v;
            m[v].push_back(x[j]);
          }
        }
      }
      cout << ans << "\n";
    }
  }
  return 0;
}
