#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int l;
    cin >> l;
    char s1[l], s2[l];
    cin >> s1;
    cin >> s2;
    int flag = 0;
    int sam[l];
    for (int i = 0; i < l; i++) {
      if (s1[i] > s2[i]) {
        flag = 1;
        break;
      }
      sam[i] = 0;
      if (s1[i] == s2[i]) {
        sam[i] = 1;
      }
    }
    if (flag) {
      cout << "-1" << endl;
      continue;
    } else {
      int cnt = 0;
      for (char w = 'a' + 0; w < 'a' + 20; w++) {
        char mc = 'z' + 1;
        int tt = 0;
        for (int i = 0; i < l; i++) {
          if (s1[i] == w && sam[i] == 0) {
            tt++;
            if (s2[i] < mc) mc = s2[i];
          }
        }
        if (tt != 0) {
          cnt++;
          for (int i = 0; i < l; i++) {
            if (s1[i] == w && sam[i] == 0) {
              s1[i] = mc;
              if (s2[i] == s1[i]) sam[i] = 1;
            }
          }
        }
      }
      cout << cnt << endl;
    }
  }
  return 0;
}
