#include #include #include #include #include #include #include #include #include using namespace std; #define int long long #define endl "\n" constexpr long long INF = (long long)1e18; constexpr long long MOD = 1'000'000'007; struct fast_io { fast_io(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); }; } fio; void pi(vector &A){ int N = A.size()/2; vector B(N * 2); for(int i = 0; i < N; i++){ B[i * 2] = A[i]; B[i * 2 + 1] = A[N + i]; } swap(A, B); } int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; } int lcm(int a, int b){ return a / gcd(a, b) * b; } long long power(long long x, long long n, long long mod){ long long ans = 1; for(;n;n>>=1,x*=x,ans%=mod,x%=mod) if(n&1)ans*=x; return ans%mod; } void exgcd(long long a, long long b, long long &x, long long &y){ if(b == 0){ x = 1; y = 0; return ; } exgcd(b, a % b, y, x); y -= a / b * x; } long long modlog(long long a, long long b, long long mod){ int ll = 1; a %= mod, b %= mod; long long low = -1, high = mod, mid; while(low + 1 < high){ mid = (low + high) >> 1; if(mid * mid >= mod) high = mid; else low = mid; } long long sqrtM = high; map mp; for(long long i = 0, x = 1; i < sqrtM; i++){ if(x == b) { if(i >= ll) return i; } if(!mp.count(x)) mp[x] = i; x = x * a % mod; } long long x, y, temp; long long A; temp = power(a, sqrtM ,mod); if(gcd(temp, mod) != 1) return -1; exgcd(temp , mod, x, y); A = (x + mod) % mod; int minimum = INF; for(long long i = 0, bA = b; i < sqrtM; i++){ if(mp.count(bA)){ long long res = i * sqrtM + mp[bA]; if(res >= ll) return res; } bA = bA * A % mod; } if(minimum != INF) return minimum; return -1; } signed main(){ cout<>T; for(int _ = 0; _ < T; _++){ vector A, B; int N; int k = -1; cin>>N; if(N == 1) cout<<1<