#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V>& x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T& x) {
  int f = 0;
  cerr << '{';
  for (auto& i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
void IO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long int n;
bool vis[20];
vector<vector<int> > graph;
long long int dfs(int i) {
  if (vis[i]) return 0;
  vis[i] = 1;
  long long int ans = 1;
  for (auto x : graph[i]) ans += dfs(x);
  return ans;
}
void solve() {
  cin >> n;
  string s1, s2;
  cin >> s1 >> s2;
  memset(vis, 0, sizeof(vis));
  graph.clear();
  graph.resize(21, vector<int>(0));
  for (int i = 0; i < n; i++) {
    if (s1[i] > s2[i]) {
      cout << "-1\n";
      return;
    } else {
      graph[s1[i] - 'a'].push_back(s2[i] - 'a');
      graph[s2[i] - 'a'].push_back(s1[i] - 'a');
    }
  }
  long long int ans = 0;
  for (int i = 0; i < 20; i++) {
    if (vis[i]) continue;
    ans += (dfs(i) - 1);
  }
  cout << ans << "\n";
}
int main() {
  IO();
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
