#include <bits/stdc++.h>
using namespace std;
char s1[100001], s2[100001];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int l;
    cin >> l;
    cin >> s1;
    cin >> s2;
    int flag = 0;
    for (int i = 0; i < l; i++) {
      if (s1[i] > s2[i]) {
        flag = 1;
        break;
      }
    }
    if (flag) {
      cout << "-1" << endl;
      continue;
    } else {
      int cnt = 0;
      vector<pair<char, char> > data;
      for (int i = 0; i < l; i++) {
        if (s1[i] != s2[i]) {
          pair<char, char> np;
          np = make_pair(s1[i], s2[i]);
          data.emplace_back(np);
        }
      }
      if (data.size() > 0) sort(data.rbegin(), data.rend());
      while (data.size() > 0) {
        cnt++;
        int r = 0;
        char bg = data[data.size() - 1].first,
             sg = data[data.size() - 1].second;
        for (int i = data.size() - 1; i >= 0; i--) {
          if (bg == data[i].first)
            r++;
          else
            break;
        }
        int r2 = 0;
        for (int i = 0; i < r; i++) {
          data[data.size() - 1 - i].first = sg;
          if (sg == data[data.size() - 1 - i].second) r2++;
        }
        for (int i = 0; i < r2; i++) data.pop_back();
        sort(data.rbegin(), data.rend());
      }
      cout << cnt << endl;
    }
  }
  return 0;
}
