1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 | // author : black_trees
#include <map>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>
#define endl '\n'
using namespace std;
using i64 = long long;
const int si = 1e5 + 10;
int n, m;
int tot = 0, tt = 0;
int head[si], hd[si];
struct Edge { int ver, Next; } e[si << 1], ee[si << 1];
inline void add(int u, int v) { e[tot] = (Edge){v, head[u]}, head[u] = tot++; }
inline void addd(int u, int v) { ee[tt] = (Edge){v, hd[u]}, hd[u] = tt++; }
int tyq = 0; // occ of all letters.
int cnt = 0, tim = 0;
std::stack<int>st;
bool ins[si];
int dfn[si], low[si];
int c[si], num[si];
void tarjan(int u) {
dfn[u] = low[u] = ++tim;
st.push(u), ins[u] = true;
for(int i = head[u]; ~i; i = e[i].Next) {
int v = e[i].ver;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(ins[v]) low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u]) {
int x; ++cnt;
do {
x = st.top(), st.pop();
ins[x] = false, c[x] = cnt, num[cnt]++;
} while(u != x);
}
}
std::map<char, int>mp, mpp;
int ind[si], clz = 0, oug[si];
std::map<std::pair<int, int>, bool> vis;
int out[si];
void Init() {
vis.clear();
mp.clear(), mpp.clear();
while(!st.empty()) st.pop();
tot = tt = clz = tyq = cnt = tim = 0;
memset(ins, false, sizeof ins);
memset(c, 0, sizeof c), memset(num, 0, sizeof num);
memset(dfn, 0, sizeof dfn), memset(low, 0, sizeof low);
memset(head, -1, sizeof head), memset(hd, -1, sizeof hd);
memset(ind, 0, sizeof ind), memset(oug, 0, sizeof oug), memset(out, 0, sizeof out);
}
int dfs(int u) {
int ret = num[u];
for(int i = hd[u]; ~i; i = ee[i].Next) {
int v = ee[i].ver;
ret += dfs(v);
}
return ret;
}
int main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit | cin.badbit);
int T; cin >> T;
while(T--) {
Init(); bool nosol = false;
string s, t;
cin >> s >> t;
n = (int)s.size();
s = ' ' + s, t = ' ' + t;
for(int i = 1; i <= n; ++i) {
if(!mpp[s[i]]) mpp[s[i]] = ++tyq;
if(!mpp[t[i]]) mpp[t[i]] = ++tyq;
}
for(int i = 1; i <= n; ++i) {
if(s[i] != t[i]) {
if(!mp[s[i]]) mp[s[i]] = ++clz;
if(!mp[t[i]]) mp[t[i]] = ++clz;
if(vis[{mp[s[i]], mp[t[i]]}]) continue;
add(mp[s[i]], mp[t[i]]), ++oug[mp[s[i]]];
vis[{mp[s[i]], mp[t[i]]}] = true;
if(oug[mp[s[i]]] > 1) {
nosol = true;
break;
}
}
}
for(int i = 1; i <= n; ++i) {
if(s[i] == t[i]) {
if(!mp[s[i]]) continue;
if(oug[mp[s[i]]]) {
nosol = true;
break;
}
}
}
if(nosol) { cout << "-1" << endl; continue; }
for(int i = 1; i <= clz; ++i) {
if(!dfn[i]) tarjan(i);
}
for(int u = 1; u <= clz; ++u) {
for(int i = head[u]; ~i; i = e[i].Next) {
int v = e[i].ver;
if(c[u] == c[v]) continue;
addd(c[v], c[u]), ++ind[c[u]], ++out[c[v]];
}
}
int ans = 0;
for(int i = 1; i <= cnt; ++i) {
if(ind[i] > 0 && num[i] > 1) {
nosol = true;
break;
}
if(!ind[i]) {
ans += dfs(i);
if(num[i] == 1) ans--;
else {
if(!out[i]) {
if(tyq < 52) ans++;
else {
nosol = true; break;
}
}
}
}
}
if(nosol) { cout << "-1" << endl; continue; }
cout << ans << endl;
}
return 0;
}
// ()()()(?`
|