#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;
    int flag = 0;
    for (int i = 0; i < n; i++) {
      if (s1[i] > s2[i]) {
        flag = 1;
        break;
      }
    }
    if (flag) {
      cout << "-1" << endl;
      continue;
    }
    int ans = 0;
    for (int i = 0; i < 26; i++) {
      char c = char(i + 'a');
      char minl = 'z';
      int p = 0;
      for (int j = 0; j < n; j++) {
        if (s1[j] == c && s1[j] != s2[j]) {
          if (s2[j] < minl) minl = s2[j];
          p = 1;
        }
      }
      for (int j = 0; j < n; j++) {
        if (s1[j] == c && s1[j] != s2[j]) {
          s1[j] = minl;
        }
      }
      ans += p;
    }
    cout << ans << endl;
  }
  return 0;
}
