#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<long long int, long long int>& a,
               const pair<long long int, long long int>& b) {
  return (a.second < b.second);
}
bool sortbys(pair<char, long long int> a, pair<char, long long int> b) {
  return a.second < b.second;
}
bool sortby(pair<long long int, long long int> a,
            pair<long long int, long long int> b) {
  if (a.second == b.second)
    return a.first > b.first;
  else
    return a.second > b.second;
}
bool sortbyfirst(pair<long long int, long long int> a,
                 pair<long long int, long long int> b) {
  return a.first < b.first;
}
long long int mpow(long long int x, long long int y) {
  long long int ans = 1;
  while (y) {
    if (y % 2 != 0) ans = (ans % 1000000007) * (x % 1000000007) % 1000000007;
    y /= 2;
    x = ((x % 1000000007) * (x % 1000000007)) % 1000000007;
  }
  return ans;
}
long long int modInverse(long long int n) { return mpow(n, 1000000007 - 2); }
bool sortbyletter(vector<char> a, vector<char> b) {
  for (int i = 0; i < a.size(); i++) {
    if (a[i] > b[i])
      return 1;
    else if (a[i] < b[i])
      return 0;
  }
  if (a.size() > b.size())
    return 1;
  else
    return 0;
}
void reverseStr(string& str) {
  int n = str.length();
  for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]);
}
long long int kadanesum(long long int arr[], long long int n) {
  long long int curr = 0, best = INT_MIN, i;
  for (i = 0; i < n; i++) {
    curr += arr[i];
    best = max(best, curr);
    if (curr < 0) curr = 0;
  }
  return best;
}
long long int nCrModPFermat(long long int n, long long int r) {
  if (r == 0) return 1;
  long long int fac[n + 1];
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % 1000000007;
  return (fac[n] * modInverse(fac[r]) % 1000000007 * modInverse(fac[n - r]) %
          1000000007) %
         1000000007;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t, i, j;
  cin >> t;
  while (t--) {
    long long int n, f = 1, ans = 0;
    cin >> n;
    string a, b;
    cin >> a >> b;
    map<char, vector<long long int>> m1;
    map<char, set<char>> m;
    for (i = 0; i < a.size(); i++) {
      if (a[i] > b[i]) f = 0;
      m1[a[i]].push_back(i);
    }
    if (f == 0) {
      cout << "-1";
    } else {
      for (i = 0; i < b.size(); i++) {
        if (a[i] != b[i]) m[b[i]].insert(a[i]);
      }
      for (auto i = m.begin(); i != m.end(); i++) {
        set<char> s;
        for (j = 0; j < a.size(); j++) {
          if ((a[j] != b[j]) && b[j] == i->first) s.insert(a[j]);
        }
        ans += s.size();
        for (auto j : s) {
          for (long long int k = 0; k < a.size(); k++) {
            if (a[k] == (j) && a[k] != b[k]) a[k] = i->first;
          }
        }
      }
      cout << ans;
    }
    cout << endl;
  }
  return 0;
}
