#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string string1, string2;
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    cin >> string1 >> string2;
    string x = "", y = "";
    bool flag = false;
    for (int i = 0; i < n; i++) {
      if (string1[i] == string2[i]) continue;
      if (string1[i] > string2[i]) {
        flag = 1;
        break;
      }
      x += string1[i];
      y += string2[i];
    }
    if (flag == true) {
      cout << -1 << '\n';
      continue;
    }
    int ans = 0;
    for (int i = 0; i < 20; i++) {
      char c = 'a' + i;
      vector<int> indexes;
      for (int i = 0; i < x.length(); i++) {
        if (x[i] == c) indexes.push_back(i);
      }
      if (indexes.size() == 0) continue;
      char chota = y[indexes[0]];
      for (int i : indexes) {
        if (chota > y[i]) chota = y[i];
      }
      for (int i : indexes) {
        x[i] = chota;
      }
      ans++;
      string xx = "", yy = "";
      for (int i = 0; i < x.length(); i++) {
        if (x[i] != y[i]) {
          xx += x[i];
          yy += y[i];
        }
      }
      x = xx;
      y = yy;
    }
    cout << ans << '\n';
  }
  return 0;
}
