1729: Color

内存限制:128 MB 时间限制:2.000 S
评测方式:文本比较 命题人:
提交:6 解决:1

题目描述

Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of the necklace can be produced. You should know that the necklace might not use up all the N colors, and the repetitions that are produced by rotation around the center of the circular necklace are all neglected. You only need to output the answer module a given number P.

输入

The first line of the input is an integer X (X <= 3500) representing the number of test cases. The following X lines each contains two numbers N and P (1 <= N <= 1000000000, 1 <= P <= 30000), representing a test case.

输出

For each test case, output one line containing the answer

样例输入 复制

5
1 30000
2 30000
3 30000
4 30000
5 30000

样例输出 复制

1
3
11
70
629