#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define ll long long
#define endl '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mem(a, b) memset(a, b, sizeof(a))
#define read(x) \
for (auto &it : x) \
cin >> it;
#define write(x) \
for (auto it : x) \
cout << it << " ";
const int MOD = 1e9 + 7;
const int N = 4e5 + 7;
ll Bs1[] = {27407, 4629423, 1629007, 8629891, 4629421, 1627979, 8627573, 8627947, 87257};
ll Bs2[] = {12345653, 28351, 45953, 75879031, 6389, 12349559, 9326651, 93255989, 2111};
ll Md1[] = {1000001329, 1745005183, 1000002239, 1345005517, 200002577, 1000003559, 1000003397, 987001789, 1000004207};
ll Md2[] = {1734557563, 1034558389, 1134559717, 1234555639, 1534556819, 1634555621, 1445003887, 1834567627, 1334558999};
ll bs1, bs2, md1, md2;
ll pow1[N], pow2[N];
ll pre1[N], pre2[N];
void pow_cal()
{
pow1[0] = pow2[0] = 1;
for (int i = 1; i < N; i++)
{
pow1[i] = (pow1[i - 1] * bs1) % md1;
pow2[i] = (pow2[i - 1] * bs2) % md2;
}
}
// build function for hash
void build_hash(string &s)
{
pre1[0] = 0, pre2[0] = 0;
for (int i = 1; i <= s.length(); i++)
{
ll x = s[i - 1] - 'a' + 1;
pre1[i] = ((pre1[i - 1] * bs1) % md1) + x;
pre1[i] %= md1;
pre2[i] = ((pre2[i - 1] * bs2) % md2) + x;
pre2[i] %= md2;
}
}
// function to get the hash value for substring l to r, here l, r is 1 based indexed
pair<ll, ll> get_hash_value(ll l, ll r)
{
if (l > r)
return {-1, -1};
int hash1 = (pre1[r] - (pre1[l - 1] * pow1[r - l + 1])) % md1;
hash1 = (hash1 + md1) % md1;
int hash2 = (pre2[r] - (pre2[l - 1] * pow2[r - l + 1])) % md2;
hash2 = (hash2 + md2) % md2;
return {hash1, hash2};
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
srand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