#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long int T;
  cin >> T;
  while (T--) {
    long long int pos[100090], top, cnt;
    char s1[100995];
    char s2[100995], ch;
    long long int flag = 0;
    long long int n;
    cin >> n;
    ;
    cin >> s1;
    cin >> s2;
    for (long long int i = 0; i < n; ++i) {
      if (s2[i] < s1[i]) {
        flag = 1;
        break;
      }
    }
    if (flag == 1) {
      cout << -1;
      cout << "\n";
    }
    if (!flag) {
      cnt = 0;
      for (long long int i = 97; i < 123; ++i) {
        ch = 125;
        top = 0;
        for (long long int j = 0; j < n; ++j) {
          if (i == s1[j] && i != s2[j]) {
            ch = min(s2[j], ch);
            pos[top++] = j;
          }
        }
        if (top >= 1) cnt += 1;
        for (long long int j = 0; j < top; ++j) s1[pos[j]] = ch;
      }
      cout << cnt;
      cout << "\n";
    }
  }
  return 0;
}
