#include <bits/stdc++.h>
using namespace std;
int dfs(int n, vector<int> edges[], unordered_map<int, int>& check,
        vector<int>& path, unordered_set<int>& vip,
        unordered_map<int, int>& distance, unordered_map<int, int>& p, int curr,
        int parent = -1) {
  int ans = -1;
  if (vip.count(curr)) ans = curr;
  p[curr] = parent;
  distance[curr] = path.size();
  path.push_back(curr);
  for (int i = 0; i < edges[curr].size(); i++) {
    int nei = edges[curr][i];
    if (nei == parent) continue;
    ans = max(ans, (dfs(n, edges, check, path, vip, distance, p, nei, curr)));
  }
  path.pop_back();
  check[curr] = ans;
  return ans;
}
void dfs2(int n, vector<int> edges[], unordered_map<int, int>& check,
          unordered_map<int, int>& near, int nearest, int curr,
          int parent = -1) {
  if (check[curr] != -1) {
    nearest = curr;
  }
  near[curr] = nearest;
  for (int i = 0; i < edges[curr].size(); i++) {
    int nei = edges[curr][i];
    if (nei == parent) continue;
    dfs2(n, edges, check, near, nearest, nei, curr);
  }
  near[curr] = nearest;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    unordered_set<long long> s;
    int flag = 0;
    vector<int> dir[20];
    for (int i = 0; i < n; i++) {
      long long aa = a[i] - 'a';
      long long bb = b[i] - 'a';
      if (aa > bb) {
        flag = 1;
        break;
      }
      if (aa == bb) continue;
      dir[aa].push_back(bb);
    }
    unordered_set<long> done;
    if (flag) {
      cout << -1 << endl;
      continue;
    }
    vector<int> c[20];
    for (int i = 0; i < n; i++) {
      if (a[i] != b[i]) c[a[i] - 'a'].push_back(i);
    }
    long long ans = 0;
    for (int i = 0; i < 20; i++) {
      int minn = INT_MAX;
      for (int j = 0; j < dir[i].size(); j++) {
        minn = min(minn, dir[i][j]);
      }
      if (minn != INT_MAX) {
        ans++;
      } else
        continue;
      for (int j = 0; j < c[i].size(); j++) {
        if (minn != b[c[i][j]] - 'a') {
          c[minn].push_back(c[i][j]);
          dir[minn].push_back(b[c[i][j]] - 'a');
        }
      }
    }
    cout << ans << endl;
  }
}
