#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  long long n;
  cin >> t;
  for (long long i = 1; i <= t; i++) {
    cin >> n;
    string a, b;
    cin >> a >> b;
    long long tab[22][22];
    memset(tab, 0, sizeof(tab));
    long long flag = 1;
    for (long long j = 0; j < n; j++) {
      if (a[j] > b[j]) {
        flag = 0;
        break;
      }
      if (a[j] < b[j]) {
        tab[b[j] - 96][a[j] - 96] = 1;
      }
    }
    if (!flag) {
      cout << -1 << '\n';
      continue;
    }
    long long sum = 0;
    for (long long col = 1; col <= 20; col++) {
      for (long long row = 1; row <= 20; row++) {
        if (tab[row][col] == 1) {
          sum++;
          for (long long j = row + 1; j <= 20; j++) {
            if (tab[j][col] == 1) {
              tab[j][row] = 1;
            }
          }
          break;
        }
      }
    }
    cout << sum << '\n';
  }
  return 0;
}
