#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const long double PI = 3.14159265358979323846;
const long double eps = 1e-5;
const long long INF = numeric_limits<int>::max();
const long long LINF = numeric_limits<long long>::max();
const long long mod = 998244353;
void solve() {
  int t;
  cin >> t;
  for (int tt = 0; tt < t; ++tt) {
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    int f = 0;
    for (int i = 0; i < n; ++i) {
      if (a[i] > b[i]) {
        cout << -1 << "\n";
        f = 1;
        break;
      }
    }
    if (f) continue;
    int res = 0;
    for (int i = 0; i < 20; ++i) {
      char x = 'z';
      for (int j = 0; j < n; ++j) {
        if (a[j] == b[j]) continue;
        if (a[j] == 'a' + i) {
          x = min(x, b[j]);
        }
      }
      if (x == 'z') continue;
      ++res;
      for (int j = 0; j < n; ++j) {
        if (a[j] == b[j]) continue;
        if (a[j] == 'a' + i) {
          a[j] = x;
        }
      }
    }
    cout << res << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve();
  return 0;
}
