#include <bits/stdc++.h>
using namespace std;
void solve() {
  int N;
  string A, B;
  cin >> N >> A >> B;
  int need[20][20] = {0};
  bool fail = false;
  for (int i = 0; i < N; i++) {
    int a = A[i] - 'a', b = B[i] - 'a';
    assert(a < 20 && b < 20);
    if (a > b) fail = true;
    if (a < b) need[a][b] = 1;
  }
  if (fail) {
    cout << -1 << endl;
    return;
  }
  int ans = 0;
  for (int i = 0; i < 20; i++) {
    int mn = 1e9;
    for (int j = i + 1; j < 20; j++)
      if (need[i][j]) mn = min(mn, j);
    if (mn == 1e9) continue;
    ans++;
    for (int j = mn + 1; j < 20; j++)
      if (need[i][j]) need[mn][j] = 1;
  }
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  while (T--) solve();
  return 0;
}
