#include <bits/stdc++.h>
std::mt19937_64 rng(
    std::chrono::steady_clock::now().time_since_epoch().count());
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    long long ans = 0;
    long long flag = 0;
    for (long long i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        flag = 1;
        break;
      }
    }
    if (flag == 1) {
      cout << "-1"
           << "\n";
      continue;
    }
    for (long long i = 0; i <= 20; i++) {
      char minm = 'z';
      vector<long long> v;
      for (long long j = 0; j < n; j++) {
        if (a[j] - 'a' == i && a[j] != b[j]) {
          minm = min(minm, b[j]);
          v.push_back(j);
        }
      }
      if (minm < 'z') {
        ans++;
        for (long long k = 0; k < v.size(); k++) a[v[k]] = minm;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
