#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
long long fast_exp(long long, long long, long long);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, ...);
int main() {
  int t;
  long long int p, a, m, v;
  scanf("%d", &t);
  while (t--) {
    scanf("%lld %lld", &a, &m);
    p = (m - 1) / 2;
    v = fast_exp(a, p, m);
    if (v == 1 || v == 0) {
      printf("YES\n");
    } else {
      printf("NO\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     11de <main+0xae>
lea    0xe97(%rip),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <fast_exp@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x1,-0x28(%rbp)
je     11bc <main+0x8c>
cmpq   $0x0,-0x28(%rbp)
jne    11cd <main+0x9d>
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
jmp    11d9 <main+0xa9>
lea    0xe3e(%rip),%rdi
call   1030 <printf@plt>
jmp    1151 <main+0x21>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
lea    0xebc(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
subl   $0x1,0xc(%rsp)
jb     11c9 <main+0x99>
lea    0xea5(%rip),%r14
lea    0x18(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xea1(%rip),%r13
lea    0xe97(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x10(%rsp),%rdx
lea    -0x1(%rdx),%rax
shr    $0x3f,%rax
lea    (%rdx,%rax,1),%rsi
add    $0xffffffffffffffff,%rsi
sar    %rsi
mov    0x18(%rsp),%rdi
call   1050 <fast_exp@plt>
cmp    $0x2,%rax
mov    %rbx,%rdi
cmovb  %r13,%rdi
call   1030 <puts@plt>
subl   $0x1,0xc(%rsp)
jae    1180 <main+0x50>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [10 x i8] c"%lld %lld\00"
@.str.2 = [5 x i8] c"YES\0A\00"
@.str.3 = [4 x i8] c"NO\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
br label %8
8: ; preds = %30, %0
%9 = load i32, i32* %2
%10 = add nsw i32 %9, -1
store i32 %10, i32* %2
%11 = icmp ne i32 %9, 0
br i1 %11, label %12, label %31
12: ; preds = %8
%13 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64* %4, i64* %5)
%14 = load i64, i64* %5
%15 = sub nsw i64 %14, 1
%16 = sdiv i64 %15, 2
store i64 %16, i64* %3
%17 = load i64, i64* %4
%18 = load i64, i64* %3
%19 = load i64, i64* %5
%20 = call i64 @fast_exp(i64 %17, i64 %18, i64 %19)
store i64 %20, i64* %6
%21 = load i64, i64* %6
%22 = icmp eq i64 %21, 1
br i1 %22, label %26, label %23
23: ; preds = %12
%24 = load i64, i64* %6
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23, %12
%27 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %30
28: ; preds = %23
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %26
br label %8
31: ; preds = %8
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [10 x i8] c"%lld %lld\00"
@str = [3 x i8] c"NO\00"
@str.4 = [4 x i8] c"YES\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i32* %1 to i8*
%5 = bitcast i64* %2 to i8*
%6 = bitcast i64* %3 to i8*
%7 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%8 = load i32, i32* %1
%9 = add nsw i32 %8, -1
store i32 %9, i32* %1
%10 = icmp eq i32 %8, 0
br i1 %10, label %24, label %11
11: ; preds = %0, %11
%12 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64* %2, i64* %3)
%13 = load i64, i64* %3
%14 = add nsw i64 %13, -1
%15 = sdiv i64 %14, 2
%16 = load i64, i64* %2
%17 = call i64 @fast_exp(i64 %16, i64 %15, i64 %13)
%18 = icmp ult i64 %17, 2
%19 = select i1 %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0)
%20 = call i32 @puts(i8* dereferenceable(1) %19)
%21 = load i32, i32* %1
%22 = add nsw i32 %21, -1
store i32 %22, i32* %1
%23 = icmp eq i32 %21, 0
br i1 %23, label %24, label %11
24: ; preds = %11, %0
ret i32 0
}