#include <bits/stdc++.h>
using namespace std;
ifstream fin("AAtest.in.txt");
long long t, n, vas, ma['t' + 1]['t' + 1];
string a, b;
bool saab;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cerr.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    cin >> a;
    cin >> b;
    saab = 1;
    for (int i = 0; i < n; i++)
      if (a[i] > b[i]) {
        saab = 0;
        break;
      }
    if (!saab) {
      cout << -1 << "\n";
      continue;
    }
    memset(ma, 0, sizeof(ma));
    for (int i = 0; i < n; i++)
      if (a[i] != b[i]) ma[a[i]][b[i]] = 1;
    for (char c = 'a'; c < 't'; c++) {
      for (char u = c + 1; u <= 't'; u++)
        if (ma[c][u]) {
          vas++;
          ma[c][u] = 0;
          for (char k = u + 1; k <= 't'; k++)
            ma[u][k] += ma[c][k], ma[c][k] = 0;
        }
    }
    cout << vas << "\n";
    vas = 0;
  }
}
