#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  string s, g;
  cin >> s >> g;
  bool pos = true;
  vector<int> v[20];
  for (int i = 0; i < n; i++) {
    if (s[i] != g[i]) v[s[i] - 97].push_back(g[i] - 97);
    if (s[i] > g[i]) pos = false;
  }
  if (!pos) {
    cout << -1 << endl;
  } else {
    int ans = 0;
    for (int k = 0; k < 20; k++) {
      int m = (int)v[k].size();
      if (m) {
        int mn = 21;
        for (int i = 0; i < m; i++) mn = min(v[k][i], mn);
        for (int i = 0; i < m; i++)
          if (v[k][i] != mn) v[mn].push_back(v[k][i]);
        ans++;
      }
    }
    cout << ans << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int z;
  cin >> z;
  for (int z1 = 1; z1 <= z; z1++) {
    solve();
  }
  return 0;
}
