#include <bits/stdc++.h>
const double pi = 3.141592653589793238;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int N = 3e5 + 5;
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 0;
  cin >> t;
  while (t--) {
    long long n = 0;
    cin >> n;
    string a, b;
    cin >> a >> b;
    char ch = 'a';
    long long ans = 0;
    bool flag = true;
    for (long long i = 0; i < 26; i++) {
      char c = 123;
      for (long long j = 0; j < n; j++) {
        if (a[j] == ch) {
          if (b[j] != a[j]) c = min(c, b[j]);
        }
      }
      if (c < ch) {
        flag = false;
        cout << "-1\n";
        break;
      }
      bool flag2 = false;
      for (long long j = 0; j < n; j++) {
        if (a[j] == ch) {
          if (b[j] != a[j]) {
            flag2 = true;
            a[j] = c;
          }
        }
      }
      if (flag2) ans++;
      ch++;
    }
    if (flag) {
      if (a == b)
        cout << ans << "\n";
      else
        cout << "-1\n";
    }
  }
}
