#include <bits/stdc++.h>
using namespace std;
long long n, m;
const int N = 1e5 + 10;
string a, b;
int parent[N];
int movex[] = {0, 0, 1, -1};
int movey[] = {1, -1, 0, 0};
int find_set(int v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) parent[b] = a;
}
void solve() {
  cin >> n;
  cin >> a >> b;
  for (int i = 0; i <= 26; i++) parent[i] = i;
  for (int i = 0; i < n; i++) {
    if (a[i] > b[i]) {
      cout << -1 << "\n";
      return;
    }
    if (a[i] != b[i]) union_sets(a[i] - 'a', b[i] - 'a');
  }
  unordered_map<int, int> cnt;
  for (int i = 0; i < 26; i++) {
    int p = find_set(i);
    cnt[p]++;
  }
  int ans = 0;
  for (auto i : cnt) {
    ans += i.second - 1;
  }
  cout << ans << "\n";
}
int main() {
  static const int _ = []() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    return 0;
  }();
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
