Lysergic Acid Diethylamide

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

might or might not had been used during making of this problem.

In this problem functions are implictly assumed to have a single non-negative integer as an argument and produce a single non-negative integer as a result.

ff is a function. f(x)=1+2++xf(x) = 1 + 2 + \ldots + x. More formally, f(x)f(x) is the sum of all positive integers less than or equal to xx.

s_ks\_k is a family of functions. s_0s\_0 is an identity function (s_0(x)=xs\_0(x) = x) and s_k(x)=s_k1(f(x)+k)s\_k(x) = s\_{k-1}(f(x) + k).

You are given tt test cases. ii-th test case contains three integers x_ix\_i, k_ik\_i and p_ip\_i. For each test case find an integer m_im\_i such that 1mp_i1-1 \leq m \leq p\_i-1, s_k_i(x_i)modp_i≢m_is\_{k\_i}(x\_i) \bmod p\_i \not \equiv m\_i. You may use m_i=1m\_i = -1 no more than 20 times. p_ip\_i are pairwise distinct. Note, that amodp0a \bmod p \geq 0, where aa is an arbitrary integer, so m_i=1m\_i = -1 is correct for any particular test case.

Why would you do that? It's simple. Finding correct answers is easy and conformist. On the other hand, finding incorrect answers is challenging and original. However, in this problem it's a bit too challenging, because of p=1p=1 case. So you decided, that you will skip some test cases with m_i=1m\_i = -1 wildcard. Sounds reasonable (not).

입력

The first line of input contains a single integer tt (1t50001 \leq t \leq 5000) --- the number of test cases.

tt lines follow. ii-th of them contains three integers x_i,k_i,p_ix\_i, k\_i, p\_i (1x_i109,0k_i105,1p_i1041 \leq x\_i \leq 10^9, 0 \leq k\_i \leq 10^5, 1 \leq p\_i \leq 10^4).

It is guaranteed that _ijp_ip_j\forall\_{i \neq j} p\_i \neq p\_j.

출력

Output tt integers. ii-th of them should be equal to m_im\_i.

The number of indices ii such that m_i=1m\_i = -1 should not exceed 20.