#include <bits/stdc++.h>
using namespace std;
void solve() {
  int a, k, i, j, n, sum = 0, mx = 0, ind, ct = 1, ans = 0;
  cin >> n;
  string A, B, C, D;
  cin >> A >> B;
  for (i = 0; i < n; i++) {
    if (A[i] > B[i]) {
      cout << -1 << endl;
      return;
    }
  }
  set<char> st;
  for (i = 97; i < 118; i++) {
    st.clear();
    char ch = i;
    for (j = 0; j < n; j++) {
      if (B[j] == ch && A[j] != B[j]) {
        st.insert(A[j]);
      }
    }
    ans += st.size();
    for (j = 0; j < n; j++) {
      if (A[j] != B[j] && st.find(A[j]) != st.end()) {
        A[j] = ch;
      }
    }
  }
  cout << ans << endl;
}
int main() {
  int tt;
  cin >> tt;
  for (int t = 0; t < tt; t++) solve();
  return 0;
}
