#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7, maxn = 1e6 + 100;
vector<int> gr[20];
int used[20];
void dfs(int v) {
  used[v] = 1;
  for (auto i : gr[v]) {
    if (!used[i]) {
      dfs(i);
    }
  }
}
void solve() {
  for (int i = 0; i < 20; ++i) gr[i].clear(), used[i] = 0;
  int n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  for (int i = 0; i < n; ++i) {
    if (a[i] > b[i]) {
      cout << "-1";
      return;
    }
  }
  for (int i = 0; i < n; ++i) {
    gr[a[i] - 'a'].push_back(b[i] - 'a');
    gr[b[i] - 'a'].push_back(a[i] - 'a');
  }
  int k = 0;
  for (int i = 0; i < 20; ++i) {
    if (!used[i]) dfs(i), k++;
  }
  cout << 20 - k;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  srand(100);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
    cout << "\n";
  }
}
