#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int t, n;
char a[N], b[N];
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> (a + 1) >> (b + 1);
    bool flag = 1;
    for (int i = 1; i <= n; ++i) {
      if (a[i] > b[i]) {
        flag = 0;
      }
    }
    if (!flag) {
      cout << -1 << endl;
      continue;
    }
    int cnt = 0;
    for (int i = 0; i <= 20; ++i) {
      char c = 'a' + i, g = 'z';
      bool f = 0;
      int num = 0;
      for (int j = 1; j <= n; ++j) {
        if (a[j] == c && b[j] != c) {
          g = min(g, b[j]);
          f = 1;
        }
      }
      if (f) cnt++;
      for (int j = 1; j <= n; ++j) {
        if (a[j] == c && b[j] != c) a[j] = g;
      }
    }
    if (!flag)
      cout << -1 << endl;
    else
      cout << cnt << endl;
  }
}
