#include <bits/stdc++.h>
using namespace std;
void dfs(vector<set<int> >& path, vector<bool>& vis, int& counter, int vertex) {
  ++counter;
  vis[vertex] = true;
  for (auto x : path[vertex]) {
    if (vis[x] == false) {
      dfs(path, vis, counter, x);
    }
  }
}
int main() {
  int t, n;
  string a, b;
  cin >> t;
  for (int t1 = 0; t1 < t; ++t1) {
    bool f = false;
    cin >> n;
    cin >> a >> b;
    vector<set<int> > path(20);
    vector<bool> visited(20, false);
    for (int i = 0; i < n; ++i) {
      if ((int)a[i] > (int)b[i]) f = true;
      if (a[i] == b[i]) continue;
      if ((int)a[i] < (int)b[i]) {
        path[(int)a[i] - 97].insert((int)b[i] - 97);
        path[(int)b[i] - 97].insert((int)a[i] - 97);
      }
    }
    if (f) {
      cout << -1 << endl;
      continue;
    }
    int res = 0;
    int q;
    for (int i = 0; i < 20; ++i) {
      if (path[i].size() > 0 && !visited[i]) {
        q = 0;
        dfs(path, visited, q, i);
        res += q - 1;
      }
    }
    cout << res << endl;
  }
}
