#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
class DSU {
 public:
  vector<int> parent;
  vector<int> sizes;
  int noOfComponenents;
  DSU(int N) {
    parent.resize(N);
    sizes.resize(N);
    noOfComponenents = N;
    for (int i = 0; i < N; ++i) {
      parent[i] = i;
      sizes[i] = 1;
    }
  }
  int find(int x) {
    if (parent[x] != x) parent[x] = find(parent[x]);
    return parent[x];
  }
  void unify(int x, int y) {
    x = find(x);
    y = find(y);
    if (isConnected(x, y)) {
      return;
    }
    --noOfComponenents;
    sizes[y] += sizes[x];
    parent[x] = y;
  }
  bool isConnected(int p, int q) { return find(p) == find(q); }
  int componentSize(int p) { return sizes[p]; }
};
void solve() {
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  DSU dsu(20);
  for (int i = 0; i < n; ++i) {
    if (s[i] == t[i]) {
      continue;
    }
    if (s[i] > t[i]) {
      cout << -1 << "\n";
      return;
    }
    dsu.unify(s[i] - 'a', t[i] - 'a');
  }
  cout << 20 - dsu.noOfComponenents << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
