#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int P = 1000000007;
const int V = 100100;
char s[V], t[V];
const int N = 20;
int vis[30][30], d[30][30], _, n;
int use[30];
int dfs(int u) {
  use[u] = 1;
  int cnt = 1;
  for (int i = 0; i < N; ++i)
    if (vis[u][i] && !use[i]) cnt += dfs(i);
  for (int i = 0; i < N; ++i)
    if (vis[i][u] && !use[i]) cnt += dfs(i);
  return cnt;
}
int main() {
  scanf("%d", &_);
  while (_--) {
    scanf("%d", &n);
    scanf("%s%s", s, t);
    memset(vis, 0, sizeof(vis));
    bool ok = true;
    for (int i = 0; i < n; ++i) {
      if (s[i] > t[i]) ok = false;
      if (s[i] < t[i]) vis[s[i] - 'a'][t[i] - 'a'] = 1;
    }
    if (!ok) {
      puts("-1");
      continue;
    }
    int ans = 0;
    memset(use, 0, sizeof(use));
    for (int i = 0; i < N; ++i) {
      if (!use[i]) ans += dfs(i) - 1;
    }
    printf("%d\n", ans);
  }
  return 0;
}
