#include <bits/stdc++.h>
using namespace std;
void testCase() {
  int n;
  cin >> n;
  char* A = new char[n];
  char* B = new char[n];
  for (int i = 0; i < n; i++) cin >> A[i];
  for (int i = 0; i < n; i++) cin >> B[i];
  bool** route = new bool*[20];
  bool** possible = new bool*[20];
  for (int i = 0; i < 20; i++) {
    route[i] = new bool[20];
    possible[i] = new bool[20];
    for (int j = 0; j < 20; j++) {
      route[i][j] = false;
      possible[i][j] = false;
    }
  }
  for (int i = 0; i < n; i++) {
    if (A[i] > B[i]) {
      cout << "-1\n";
      return;
    } else if (A[i] < B[i]) {
      route[A[i] - 'a'][B[i] - 'a'] = true;
    }
  }
  int answer = 0;
  for (int i = 18; i >= 0; i--) {
    for (int j = i; j < 20; j++) {
      if (route[i][j] && !(possible[i][j])) {
        answer++;
        int next = j;
        for (int ctr = i; ctr < j; ctr++) {
          if (possible[ctr][j]) {
            next = ctr;
            break;
          }
        }
        possible[i][next] = true;
        for (int toAdd = next; toAdd < 20; toAdd++) {
          if (possible[next][toAdd]) {
            possible[i][toAdd] = true;
          }
        }
      }
    }
  }
  cout << answer << "\n";
  return;
}
int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    testCase();
  }
  return 0;
}
