#include <bits/stdc++.h>
using namespace std;
struct _IO {
  _IO() {
    ios::sync_with_stdio(0);
    cin.tie(0);
  }
} _io;
const int N = 2e6 + 5, M = 1e9 + 7;
int fa[N];
int fi(int x) { return fa[x] == x ? x : fa[x] = fi(fa[x]); }
int solve() {
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  int ans = 0;
  for (int i = 0; i <= 20; i++) fa[i] = i;
  for (int i = 0; i < n; i++) {
    if (s[i] > t[i]) return -1;
    int x = fi(s[i] - 'a'), y = fi(t[i] - 'a');
    if (x != y) ans++;
    fa[x] = y;
  }
  return ans;
}
int main() {
  int tt;
  cin >> tt;
  while (tt--) cout << solve() << '\n';
}
