#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
class UnionFind {
 private:
  vec<int> p, s;
  int cnt;

 public:
  UnionFind() {}
  UnionFind(int N) : cnt(N), s(N, 1), p(N) { iota(p.begin(), p.end(), 0); }
  int find(int x) {
    if (p[x] == x)
      return x;
    else
      return p[x] = find(p[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (s[x] > s[y]) swap(x, y);
    p[x] = y;
    s[y] += s[x];
    cnt--;
  }
  bool is_same_set(int x, int y) { return find(x) == find(y); }
  int size(int x) { return s[find(x)]; }
  int compnents_number() { return cnt; }
};
void solve() {
  int N;
  cin >> N;
  string S, T;
  cin >> S >> T;
  int M = 20;
  UnionFind uf(M);
  for (int i = 0; i < N; i++) {
    if (S[i] > T[i]) {
      cout << -1 << "\n";
      return;
    }
    uf.unite(S[i] - 'a', T[i] - 'a');
  }
  int ans = 0;
  vec<int> used(M);
  for (int i = 0; i < M; i++) {
    int n = uf.find(i);
    if (used[n]) continue;
    ans += uf.size(n) - 1;
    used[n] = 1;
  }
  cout << ans << "\n";
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) {
    solve();
  }
}
