#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, i, j, k, l, t, x, y;
  string str = "abcdefghijklmnopqrst";
  cin >> t;
  while (t--) {
    cin >> n;
    string a, b;
    cin >> a >> b;
    long long int f = 0;
    for (i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        f = 1;
        break;
      }
    }
    if (f) {
      cout << -1 << endl;
      continue;
    }
    long long int sum = 0;
    for (j = 0; j < 20; j++) {
      char ch = str[j];
      char c = 'z';
      long long int qq = 0;
      for (i = 0; i < n; i++) {
        if (a[i] == ch && a[i] != b[i]) {
          qq = 1;
          if (b[i] < c) {
            c = b[i];
          }
        }
      }
      sum += qq;
      for (l = 0; l < n; l++) {
        if (a[l] == ch && a[l] != b[l]) {
          a[l] = c;
        }
      }
      if (a == b) break;
    }
    cout << sum << endl;
  }
}
