#include <bits/stdc++.h>
using namespace std;
const int lets = 20;
vector<int> adj[lets];
bool visited[lets];
int n;
string a, b;
void dfs(int v) {
  visited[v] = true;
  for (int next : adj[v])
    if (!visited[next]) dfs(next);
}
bool isPossible() {
  for (int i = 0; i < n; i++) {
    int first = a[i] - 'a', second = b[i] - 'a';
    if (first > second) return false;
    adj[a[i] - 'a'].push_back(b[i] - 'a');
    adj[b[i] - 'a'].push_back(a[i] - 'a');
  }
  return true;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    cin >> n >> a >> b;
    for (int i = 0; i < 20; i++) adj[i].clear();
    if (!isPossible()) {
      cout << -1 << endl;
      continue;
    }
    memset(visited, false, sizeof visited);
    int ans = 20;
    for (int i = 0; i < 20; i++) {
      if (!visited[i]) {
        dfs(i);
        ans--;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
