#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string s1, s2;
    cin >> s1 >> s2;
    bool poss = true;
    int change[20][20];
    memset(change, 0, sizeof(change));
    for (int i = 0; i < n; i++) {
      if (s1[i] > s2[i])
        poss = false;
      else {
        if (s1[i] != s2[i]) {
          change[s1[i] - 97][s2[i] - 97]++;
        }
      }
    }
    if (!poss) {
      cout << -1 << endl;
      continue;
    }
    int steps = 0;
    for (int i = 0; i < 19; i++) {
      int x = -1;
      for (int j = 1; j < 20; j++) {
        if (change[i][j] > 0) {
          steps++;
          x = j;
          break;
        }
      }
      if (x != -1) {
        for (int j = x + 1; j <= 20; j++) {
          change[x][j] += change[i][j];
        }
      }
    }
    cout << steps << endl;
  }
}
