#include <bits/stdc++.h>
const long long mod = 1e9 + 7;
using namespace std;
long long int power(long long int a, long long int b) {
  long long int res = 1;
  while (b > 0) {
    if (b % 2 == 1) res = res * a;
    a = a * a;
    b = b / 2;
  }
  return res;
}
long long po(long long x, long long y) {
  long long res = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1) res = ((res % mod) * (x % mod)) % mod;
    y = y >> 1;
    x = ((x % mod) * (x % mod)) % mod;
  }
  return res;
}
void dfs(bool visited[], vector<int> adj[], int node, int &ans) {
  if (visited[node]) return;
  visited[node] = true;
  for (int i = 0; i < adj[node].size(); i++) {
    if (visited[adj[node][i]])
      continue;
    else {
      ans++;
      dfs(visited, adj, adj[node][i], ans);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    int arr[20] = {0};
    int flag = 1;
    for (int i = 0; i < n; i++) {
      if (a[i] - 'a' > b[i] - 'a') {
        flag = 0;
        break;
      }
    }
    if (flag == 0)
      cout << "-1" << endl;
    else {
      int ans = 0;
      for (int i = 0; i < 20; i++) {
        int mi = 30;
        for (int j = 0; j < n; j++) {
          if (b[j] == a[j]) continue;
          if (a[j] == i + 'a') mi = min(mi, b[j] - 'a');
        }
        for (int j = 0; j < n; j++)
          if (a[j] == i + 'a' && a[j] != b[j]) {
            a[j] = mi + 'a';
          }
        if (mi != 30) ans++;
      }
      cout << ans << endl;
    }
  }
}
