#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void ckmax(T& x, T y) {
  x = (y > x ? y : x);
}
template <typename T>
inline void ckmin(T& x, T y) {
  x = (y < x ? y : x);
}
const int MAXN = 1e5, MAXM = 20;
int n;
char a[MAXN + 5];
char b[MAXN + 5];
int cnt[MAXM + 5][MAXM + 5];
bool go[MAXM + 5][MAXM + 5];
void solve_case() {
  cin >> n;
  cin >> (a + 1);
  cin >> (b + 1);
  memset(cnt, 0, sizeof(cnt));
  for (int i = 1; i <= n; ++i) {
    if (a[i] > b[i]) {
      cout << -1 << endl;
      return;
    }
    if (a[i] != b[i]) {
      cnt[a[i] - 'a'][b[i] - 'a']++;
    }
  }
  memset(go, 0, sizeof(go));
  int ans = 0;
  for (int i = MAXM - 1; i >= 0; --i) {
    for (int j = i + 1; j < MAXM; ++j) {
      if (cnt[i][j]) {
        if (!go[i][j]) {
          ans++;
          for (int k = i + 1; k < j; ++k)
            if (go[k][j]) {
              go[i][k] = 1;
              for (int l = k + 1; l < MAXM; ++l) go[i][l] |= go[k][l];
            }
        }
        go[i][j] = 1;
        for (int k = j + 1; k < MAXM; ++k) go[i][k] |= go[j][k];
      }
    }
  }
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    solve_case();
  }
  return 0;
}
