#include <bits/stdc++.h>
using namespace std;
using gg = long long;
constexpr gg MAX = 1e6 + 5;
constexpr gg mod = 1e9 + 7;
constexpr gg INF = 1e10;
constexpr double thre = 1e-7;
gg ti, ni, mi, ki, di, pi, xi, yi;
class UFS {
 public:
  UFS(gg n) : ufs(n) { iota(begin(ufs), end(ufs), 0); }
  gg findRoot(gg x) { return ufs[x] == x ? x : ufs[x] = findRoot(ufs[x]); }
  bool unionSets(gg a, gg b) {
    a = findRoot(a), b = findRoot(b);
    if (a == b) {
      return false;
    }
    ufs[a] = b;
    return true;
  }

 private:
  vector<gg> ufs;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ti = 1;
  cin >> ti;
  while (ti--) {
    string a, b;
    cin >> ni >> a >> b;
    gg ans = 0;
    UFS ufs(26);
    for (gg i = 0; i < ni; ++i) {
      if (a[i] > b[i]) {
        ans = -1;
        break;
      }
      if (ufs.unionSets(a[i] - 'a', b[i] - 'a')) {
        ans++;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
