#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long t;
  cin >> t;
  for (long long x = 0; x < t; ++x) {
    long long n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    bool possible = true;
    for (long long i = 0; i < n; ++i) {
      if (a[i] > b[i]) {
        possible = false;
        break;
      }
    }
    if (!possible) {
      cout << "-1\n";
      continue;
    }
    long long ans = 0;
    for (char i = 'a'; i != 'u'; i++) {
      vector<long long> idx;
      char mn = 'u';
      for (long long j = 0; j < n; j++) {
        if (a[j] != b[j] and a[j] == i) {
          idx.push_back(j);
          mn = min(mn, b[j]);
        }
      }
      if (!idx.empty()) {
        ans++;
        for (long long j : idx) {
          a[j] = mn;
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
