#include <bits/stdc++.h>
using namespace std;
int n, m, k, t, l;
int a[100];
int main() {
  scanf("%d", &t);
  for (; t > 0; t--) {
    scanf("%d", &n);
    char A[100001], B[100001];
    scanf("%s", A);
    scanf("%s", B);
    set<pair<int, int> > to;
    bool isPossible = true;
    for (int i = 0; i < n; i++) {
      if (A[i] > B[i]) {
        isPossible = false;
        break;
      }
      if (A[i] < B[i]) {
        to.insert({A[i], B[i]});
      }
    }
    if (!isPossible) {
      printf("-1\n");
      continue;
    }
    int cnt = 0;
    while (!to.empty()) {
      pair<int, int> that = *to.begin();
      int nxt = that.second;
      to.erase(that);
      cnt++;
      while (true) {
        if (to.begin() == to.end()) break;
        pair<int, int> ddul = *to.begin();
        if (ddul.first != that.first) break;
        to.insert({nxt, ddul.second});
        to.erase(ddul);
      }
    }
    printf("%d\n", cnt);
  }
}
