#include <bits/stdc++.h>
using namespace std;
void boost() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
int main() {
  boost();
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    string s1, s2;
    cin >> s1 >> s2;
    long long int i, flag = 0;
    for (i = 0; i < n; i++)
      if (s1[i] > s2[i]) flag = 1;
    if (flag) {
      cout << "-1" << endl;
      continue;
    }
    long long int ans = 0;
    for (char c = 'a'; c <= 't'; c++) {
      map<long long int, long long int> mp;
      for (i = 0; i < n; i++) {
        if (s1[i] != c && s2[i] == c) {
          if (mp[s1[i]]) continue;
          mp[s1[i]]++;
          ans++;
        }
      }
      for (i = 0; i < n; i++) {
        if (s2[i] == c || (mp[s1[i]] && c <= s2[i])) {
          s1[i] = c;
        }
      }
    }
    cout << ans << endl;
  }
}
