#include <bits/stdc++.h>
using namespace std;
const int maxx = 200010;
vector<int> g[31];
int t, n, m;
int ans = 0, total = 0, sum = 0, cnt = 0;
int a[maxx], fa[31], vis[31], mark[31], siz[31];
int bj[31][31];
char c1[maxx], c2[maxx];
struct node {
  int x;
  int y;
} s[maxx];
bool cmp(node a, node b) {
  if (a.x == b.y) return a.y < b.y;
  return a.x < b.x;
}
int find(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
void dfs(int x) {
  for (register int i = 0; i < g[x].size(); i++) {
    int to = g[x][i];
    if (mark[to]) continue;
    mark[to] = 1;
    int X = find(x), Y = find(to);
    if (X != Y) {
      fa[X] = Y;
      siz[Y] += siz[X] + 1;
      siz[X] = 0;
    }
    dfs(to);
  }
}
int main() {
  scanf("%d", &t);
  while (t--) {
    memset(bj, 0, sizeof(bj));
    memset(vis, 0, sizeof(vis));
    for (register int i = 0; i <= 25; i++) {
      g[i].clear();
      fa[i] = i;
      siz[i] = 0;
    }
    scanf("%d ", &n);
    scanf("%s", c1 + 1);
    scanf("%s", c2 + 1);
    ans = 0;
    total = 0;
    sum = 0;
    for (register int i = 1; i <= n; i++) {
      if (c1[i] > c2[i]) {
        ans = 1;
        break;
      }
      if (c1[i] != c2[i] && !bj[c1[i] - 'a'][c2[i] - 'a'])
        g[c1[i] - 'a'].push_back(c2[i] - 'a');
    }
    if (ans)
      printf("-1\n");
    else {
      for (register int i = 0; i <= 25; i++) sort(g[i].begin(), g[i].end());
      for (register int i = 1; i <= n; i++)
        s[i].x = c1[i] - 'a', s[i].y = c2[i] - 'a';
      for (register int i = 0; i <= 25; i++)
        if (!vis[i]) {
          memset(mark, 0, sizeof(mark));
          dfs(i);
          for (register int j = 0; j <= 25; j++) vis[i] = mark[i];
        }
      for (register int i = 0; i <= 25; i++) {
        ans += siz[i];
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
