#P2350. [HAOI2012] 外星人

[HAOI2012] 外星人

Description

Elio found a number NN on her quilt. She wants to find the smallest xx such that φx(N)=1\varphi^x(N) = 1. Based on this xx, she can find clues about the aliens who once abducted her. Of course, she will not do the computation herself; please help her compute the minimal xx.

φx(N)\varphi^x(N) denotes iterating φ\varphi xx times, not an exponent.

Input Format

The first line contains a positive integer test\mathrm{test}. Then for each of the test\mathrm{test} test cases, the first line contains a positive integer mm, followed by mm lines, each containing two positive integers pi,qip_i, q_i.

Here i=1mpiqi\displaystyle \prod_{i = 1}^{m} p_i^{q_i} is the standard factorization form of NN, and \prod denotes a product.

Output Format

Output test\mathrm{test} lines, each containing one integer, the answer.

1
2
2 2
3 1

3

Hint

φ\varphi is Euler's totient function. φ(n)\varphi(n) is the number of integers not exceeding nn that are coprime to nn. Hint: $\varphi(\prod_{i=1}^mp_i^{q_i})=\prod_{i=1}^m(p_i-1)\times p_i^{q_i-1}$.

Constraints

  • For 30% of the testdata, N106N \le 10^6.
  • For 60% of the testdata, x100x \le 100.
  • For 100% of the testdata, test50\mathrm{test} \le 50, 1pi1051 \le p_i \le {10}^5, 1qi1091 \le q_i \le {10}^9, m2000m \le 2000.

Translated by ChatGPT 5