#P13953. [ICPC 2023 Nanjing R] 原根

[ICPC 2023 Nanjing R] 原根

Description

BaoBao has just learnt about primitive roots in number theory and is showing off his knowledge to Little Cyan Fish through an instant messaging software.

:::align{center}

This image is only for amusement and has nothing to do with the problem itself. You can safely skip this image if you can't read Chinese. :::

Based on the fact that if a non-negative integer gg is a primitive root modulo PP (where PP is a prime), then gP11(modP)g^{P - 1} \equiv 1 \pmod{P}, BaoBao decided to use the expression (g  ˆ(P - 1)) % P == 1\texttt{(g \^ (P - 1)) \% P == 1} to check if gg is a primitive root modulo PP. Unfortunately, in most programming languages (for example C and C++),  ˆ\texttt{\^ } is the bitwise exclusive-or (XOR) operator, not the power operator. Little Cyan Fish spotted this issue at once and now he is interested in the following problem:

Given a prime number PP and a non-negative integer mm, how many non-negative integers gg satisfies gmg \leq m and g(P1)1(modP)g \oplus (P-1) \equiv 1 \pmod{P}? Here \oplus is bitwise exclusive-or (XOR) operator.

Please help Little Cyan Fish solve this problem.

Input Format

There are multiple test cases. The first line of the input contains an integer TT (1T1051 \leq T \leq 10^{5}) indicating the number of test cases. For each test case:

The first and only line contains two integers PP and mm (2P10182 \le P \le 10^{18}, 0m10180 \leq m \leq 10^{18}, PP is a prime).

Output Format

For each test case, output one line containing one integer indicating the number of gg satisfying the constraints.

3
2 0
7 11
1145141 998244353
1
2
872