#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
void solve() {
  int n;
  scanf("%d", &n);
  string a, b;
  cin >> a >> b;
  int counter = 0;
  int ok = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] > b[i]) ok = 0;
  }
  if (!ok) {
    printf("-1\n");
    return;
  }
  for (int i = 0; i < 20; i++) {
    char MIN = 'z';
    for (int j = 0; j < n; j++) {
      if (a[j] == 'a' + i && a[j] != b[j]) MIN = min(MIN, b[j]);
    }
    for (int j = 0; j < n; j++) {
      if (a[j] == 'a' + i && a[j] != b[j]) a[j] = MIN;
    }
    if (MIN != 'z') ++counter;
  }
  printf("%d\n", counter);
  return;
}
int main() {
  int _;
  scanf("%d", &_);
  while (_--) solve();
}
