#include <bits/stdc++.h>
using namespace std;
void DBG() { cerr << "]\n"; }
template <typename T, typename... Args>
void DBG(T first, Args... args) {
  cerr << first;
  if (sizeof...(args)) cerr << ", ";
  DBG(args...);
}
long long int del_x[4] = {-1, 1, 0, 0};
long long int del_y[4] = {0, 0, 1, -1};
long long int XX[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
long long int YY[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
void solve() {
  long long int n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  long long int chance = 0;
  for (long long int i = 0; i < n; i++) {
    if (a[i] > b[i]) {
      cout << "-1\n";
      return;
    }
  }
  for (char ch = 'a'; ch <= 't'; ch++) {
    char mn = 'z';
    vector<long long int> pos;
    for (long long int i = 0; i < n; i++) {
      if (a[i] == ch && b[i] > ch) {
        mn = min(mn, b[i]);
        pos.push_back(i);
      }
    }
    if (!pos.empty()) {
      chance++;
      for (auto p : pos) {
        a[p] = mn;
      }
    }
  }
  cout << chance << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long int t = 1;
  cin >> t;
  for (long long int tt = 1; tt <= t; tt++) {
    solve();
  }
}
