//Let's join Kaede Takagaki Fan Club !! #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; //#define int long long #define L __int128 typedef long long ll; typedef pair P; typedef pair P1; typedef pair P2; #define pu push #define pb push_back #define eb emplace_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define a first #define b second #define fi first #define sc second #define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define rep(i,x) for(int i=0;i bool chmax(t&a,u b){if(a bool chmin(t&a,u b){if(b using vc=vector; template ostream& operator<<(ostream& os,const pair& p){ return os<<"{"< ostream& operator<<(ostream& os,const vc& v){ os<<"{"; for(auto e:v)os< void g(T &a){ cin >> a; } template void o(const T &a,bool space=false){ cout << a << (space?' ':'\n'); } //ios::sync_with_stdio(false); const ll mod = 1000000007;//998244353 mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); template void add(T&a,T b){ a+=b; if(a >= mod) a-=mod; } ll modpow(ll x,ll n){ ll res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%mod; n>>=1; } return res; } #define _sz 1 ll F[_sz],R[_sz]; void make(){ F[0] = 1; for(int i=1;i<_sz;i++) F[i] = F[i-1]*i%mod; R[_sz-1] = modpow(F[_sz-1], mod-2); for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1) % mod; } ll C(int a,int b){ if(b < 0 || a < b) return 0; return F[a]*R[b]%mod*R[a-b]%mod; } //ID * 30 + char -> next_ID unordered_maphsh[500005]; int nxt = 1; int num[2][1000005]; constexpr int maxn = 1000005, alpha = 26; struct eertree{ int len[maxn], link[maxn], to[maxn][alpha], cnt[maxn], id[maxn]; int slink[maxn], diff[maxn]; int sz, last, n; char ch[maxn]; vcedge[maxn]; //0...「0」の頂点 //1...「-1」の頂点 //len... その頂点番号の文字列の長さ //link... suffix linkを1つたどった先 //to... 今の状態からこの文字を足すとどこに行きますか? //diff...len[x]-len[link[x]] //slink... diffが切り替わるところ (diffが同じところをぶっ飛ばすとO(log N)になる) void init(){ n = 0; last = 0; ch[n++] = -1; link[0] = 1; edge[1].pb(0); len[1] = -1; sz = 2; } //len[1] = -1なので少なくとも「-1」では止まる //なかなかやる int get_link(int v){ while(ch[n-len[v]-2] != ch[n-1]) v = link[v]; return v; } //基本的にインクリメンタルな仕様が想定されていそう void add_letter(char c){ ch[n++] = c -= 'A'; //まずこの末端がもつ最長の回文を見つけようね! last = get_link(last); int cur_id; if(hsh[len[last]+1].find(id[last] * 30 + c) == hsh[len[last]+1].end()){ hsh[len[last]+1][id[last] * 30 + c] = nxt ++; } cur_id = hsh[len[last]+1][id[last] * 30 + c]; //この遷移は初めてか?力抜けよ if(!to[last][c]){ //ここら辺はうんという感じで len[sz] = len[last] + 2; link[sz] = to[get_link(link[last])][c]; edge[link[sz]].pb(sz); diff[sz] = len[sz] - len[link[sz]]; //slinkを踏むと1系列ぶっ飛ばせる //つよいね~ if(diff[sz] == diff[link[sz]]){ slink[sz] = slink[link[sz]]; } else{ slink[sz] = link[sz]; } //ここもまあうん to[last][c] = sz++; } last = to[last][c]; cnt[last] ++; id[last] = cur_id; } void up(int wh, int v){ for(auto i:edge[v]){ up(wh, i); cnt[v] += cnt[i]; } num[wh][id[v]] = cnt[v]; } }k, f; void solve(){ string a, b; cin >> a >> b; k.init(); f.init(); for(auto c:a) k.add_letter(c); for(auto c:b) f.add_letter(c); k.up(0, 1); f.up(1, 1); ll ans = 0; repn(i, 1000004) ans += 1LL * num[0][i] * num[1][i]; o(ans); } signed main(){ cin.tie(0); ios::sync_with_stdio(0); cout<> t; while(t--) solve(); }