#P4139. 上帝与集合的正确用法

上帝与集合的正确用法

Description

According to some books, one of God’s failed attempts at creation went like this:

On the first day, God created a basic element of the world, called "yuan" (pinyin).

On the second day, God created a new element, called α\alpha. α\alpha is defined as a set composed of "yuan". It is easy to see that there are exactly two distinct α\alpha.

On the third day, God created another new element, called β\beta. β\beta is defined as a set composed of α\alpha. It is easy to see that there are four distinct β\beta.

On the fourth day, God created a new element, γ\gamma. γ\gamma is defined as a set of β\beta. Clearly, there are 1616 distinct γ\gamma.

If this continues, the fifth kind of element will have 6553665536 types, and the sixth kind will have 2655362^{65536} types. This will be an astronomical number.

However, God did not anticipate how fast the number of element types would grow. He wanted to enrich the world’s elements, so day after day, year after year, he kept creating new elements…

Not long after, when God created the last kind of element θ\theta, he found that there were so many elements that the world’s capacity was insufficient to bear them. So that day, God destroyed the world.

To this day, God still remembers that failed act of creation. Now he wants to ask you: how many distinct elements of type θ\theta are there?

God thinks this number may be too huge to represent, so you only need to report it modulo pp.

You may assume that from α\alpha to θ\theta, God created elements for 10910^9 steps, or 101810^{18} steps, or simply \infty steps.

In short:

Define a0=1,an=2an1a_0=1,a_n=2^{a_{n-1}}. It can be proved that bn=anmodpb_n=a_n\bmod p becomes constant after some index; find that value.

Input Format

The first line contains an integer TT, the number of test cases.

Each of the next TT lines contains one positive integer pp, the modulus.

Output Format

Output TT lines, each containing one positive integer, which is the answer modulo pp.

3
2
3
6
0
1
4

Hint

For 100%100\% of the testdata, T103T\le 10^3, p107p\le 10^7.

Translated by ChatGPT 5