#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (!a) return b;
  return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
const long long N = 3e5;
const long long mod = 1e9 + 7;
const long long M = 1009;
void sout() { cout << "\n"; }
template <typename T, typename... Types>
void sout(T var1, Types... var2) {
  cout << var1 << " ";
  sout(var2...);
}
template <typename T>
void sout(vector<T> arr) {
  for (auto it : arr) {
    cout << it << " ";
  }
  sout();
}
vector<long long> parent(40, -1);
vector<long long> size(40, 0);
long long find_set(long long v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set(parent[v]);
}
void make_set(long long v) {
  parent[v] = v;
  size[v] = 1;
}
void union_sets(long long a, long long b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (size[a] < size[b]) swap(a, b);
    parent[b] = a;
    size[a] += size[b];
  }
}
void test_case() {
  long long n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  parent.assign(40, -1);
  size.assign(40, 0);
  vector<pair<long long, long long>> ed;
  for (long long i = 0; i < n; i++) {
    if (s[i] != t[i] && s[i] < t[i]) {
      ed.push_back({s[i] - 'a', t[i] - 'a'});
    } else if (s[i] != t[i] && s[i] > t[i]) {
      cout << -1 << "\n";
      return;
    }
  }
  sort(ed.begin(), ed.end(), greater<pair<long long, long long>>());
  long long ans = 0;
  for (long long i = 0; i <= 20; i++) {
    make_set(i);
  }
  for (auto it : ed) {
    long long a = it.first;
    long long b = it.second;
    if (find_set(a) != find_set(b)) {
      ans++;
      union_sets(find_set(a), find_set(b));
    }
  }
  cout << ans << "\n";
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  long long t = 1;
  cin >> t;
  while (t--) {
    test_case();
  }
}
