#include <bits/stdc++.h>
using namespace std;
long long int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
long long int dy[] = {0, -1, -1, -1, 0, 1, 1, 1};
long long int dxx[] = {1, -1, 0, 0};
long long int dyy[] = {0, 0, -1, 1};
long long int modexpo(long long int x, long long int y) {
  if (y == 0) return 1;
  if (y % 2) {
    long long int viky = modexpo(x, y / 2);
    return (((x * viky) % 998244353) * viky) % 998244353;
  } else {
    long long int viky = modexpo(x, y / 2);
    return (viky * viky) % 998244353;
  }
}
bool isvalid(long long int x, long long int y, long long int n,
             long long int m) {
  return (x >= 0 && x < n && y >= 0 && y < m);
}
vector<long long int> v[100011];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    string a;
    string b;
    cin >> a >> b;
    long long int ok = 1;
    long long int count1 = 0;
    for (long long int i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        ok = 0;
        break;
      } else if (a[i] == b[i]) {
        count1++;
      }
    }
    set<string> st;
    if (!ok) {
      cout << "-1\n";
    } else {
      ok = 1;
      long long int ans = 0;
      long long int p = 100001;
      while (p--) {
        long long int count = 0;
        ans++;
        char f = 'z';
        for (long long int i = 0; i < n; i++) {
          if (a[i] == b[i]) {
            continue;
          }
          if (f > a[i]) {
            f = a[i];
          }
        }
        char s = 'z';
        for (long long int j = 0; j < n; j++) {
          if (a[j] == b[j]) {
            continue;
          }
          if (a[j] == f) {
            if (s > b[j]) {
              s = b[j];
            }
          }
        }
        for (long long int i = 0; i < n; i++) {
          if (a[i] == b[i]) {
            continue;
          }
          if (f == a[i]) {
            a[i] = s;
          }
        }
        for (long long int i = 0; i < n; i++) {
          if (a[i] == b[i]) {
            count++;
          }
        }
        if (count == n) {
          ok = 0;
          break;
        }
      }
      cout << min(ans, n - count1) << "\n";
    }
  }
  return 0;
}
