#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 mul(long long);
int /*<<< orphan*/ printf(char *, long long);
int /*<<< orphan*/ scanf(char *, long long *, ...);
main() {
  long long int t, n, m, q, r, i, j, p;
  scanf("%lld", &t);
  for (i = 0; i < t; i++) {
    scanf("%lld%lld%lld", &n, &m, &q);
    for (j = 0; j < q; j++) {
      scanf("%lld", &r);
      p = (mul(n) / (mul(n - r) * mul(r))) % m;
      printf("%lld\n", p);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xec2(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    1225 <main+0xf5>
lea    0xe92(%rip),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rcx
mov    $0x0,%al
call   1040 <scanf@plt>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    120f <main+0xdf>
lea    0xe6a(%rip),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <mul@plt>
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
sub    -0x30(%rbp),%rdi
call   1050 <mul@plt>
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <mul@plt>
mov    -0x58(%rbp),%rcx
mov    %rax,%rdx
mov    -0x50(%rbp),%rax
imul   %rdx,%rcx
cqto
idiv   %rcx
cqto
idivq  -0x20(%rbp)
mov    %rdx,-0x48(%rbp)
mov    -0x48(%rbp),%rsi
lea    0xe14(%rip),%rdi
call   1030 <printf@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1189 <main+0x59>
jmp    1214 <main+0xe4>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1159 <main+0x29>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    0xec3(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpq   $0x0,0x20(%rsp)
jle    1267 <main+0x137>
lea    0xea4(%rip),%rbx
lea    0x8(%rsp),%rbp
lea    0xe9d(%rip),%r14
xor    %eax,%eax
jmp    1194 <main+0x64>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x28(%rsp),%rax
add    $0x1,%rax
cmp    0x20(%rsp),%rax
jge    1267 <main+0x137>
mov    %rax,0x28(%rsp)
lea    0xe60(%rip),%rdi
lea    0x18(%rsp),%rsi
lea    0x30(%rsp),%rdx
lea    0x10(%rsp),%rcx
xor    %eax,%eax
call   1040 <scanf@plt>
cmpq   $0x0,0x10(%rsp)
jle    1180 <main+0x50>
xor    %r13d,%r13d
jmp    11eb <main+0xbb>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cqto
idiv   %rcx
mov    %rdx,%rsi
mov    %r14,%rdi
call   1030 <printf@plt>
add    $0x1,%r13
cmp    0x10(%rsp),%r13
jge    1180 <main+0x50>
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x18(%rsp),%rdi
call   1050 <mul@plt>
mov    %rax,%r15
mov    0x18(%rsp),%rdi
sub    0x8(%rsp),%rdi
call   1050 <mul@plt>
mov    %rax,%r12
mov    0x8(%rsp),%rdi
call   1050 <mul@plt>
mov    %rax,%rcx
imul   %r12,%rcx
mov    %r15,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     1240 <main+0x110>
mov    %r15,%rax
cqto
idiv   %rcx
jmp    1247 <main+0x117>
xchg   %ax,%ax
mov    %r15d,%eax
xor    %edx,%edx
div    %ecx
mov    0x30(%rsp),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
jne    11d0 <main+0xa0>
xor    %edx,%edx
div    %ecx
mov    %edx,%esi
jmp    11d8 <main+0xa8>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [13 x i8] c"%lld%lld%lld\00"
@.str.2 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 0, i32* %1
%10 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %2)
store i64 0, i64* %7
br label %11
11: ; preds = %41, %0
%12 = load i64, i64* %7
%13 = load i64, i64* %2
%14 = icmp slt i64 %12, %13
br i1 %14, label %15, label %44
15: ; preds = %11
%16 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64* %3, i64* %4, i64* %5)
store i64 0, i64* %8
br label %17
17: ; preds = %37, %15
%18 = load i64, i64* %8
%19 = load i64, i64* %5
%20 = icmp slt i64 %18, %19
br i1 %20, label %21, label %40
21: ; preds = %17
%22 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %6)
%23 = load i64, i64* %3
%24 = call i64 @mul(i64 %23)
%25 = load i64, i64* %3
%26 = load i64, i64* %6
%27 = sub nsw i64 %25, %26
%28 = call i64 @mul(i64 %27)
%29 = load i64, i64* %6
%30 = call i64 @mul(i64 %29)
%31 = mul nsw i64 %28, %30
%32 = sdiv i64 %24, %31
%33 = load i64, i64* %4
%34 = srem i64 %32, %33
store i64 %34, i64* %9
%35 = load i64, i64* %9
%36 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %35)
br label %37
37: ; preds = %21
%38 = load i64, i64* %8
%39 = add nsw i64 %38, 1
store i64 %39, i64* %8
br label %17
40: ; preds = %17
br label %41
41: ; preds = %40
%42 = load i64, i64* %7
%43 = add nsw i64 %42, 1
store i64 %43, i64* %7
br label %11
44: ; preds = %11
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [13 x i8] c"%lld%lld%lld\00"
@.str.2 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = bitcast i64* %1 to i8*
%7 = bitcast i64* %2 to i8*
%8 = bitcast i64* %3 to i8*
%9 = bitcast i64* %4 to i8*
%10 = bitcast i64* %5 to i8*
%11 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %1)
%12 = load i64, i64* %1
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %42
14: ; preds = %0, %38
%15 = phi i64 [ %39, %38 ], [ 0, %0 ]
%16 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64* %2, i64* %3, i64* %4)
%17 = load i64, i64* %4
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %38
19: ; preds = %14, %19
%20 = phi i64 [ %35, %19 ], [ 0, %14 ]
%21 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %5)
%22 = load i64, i64* %2
%23 = call i64 @mul(i64 %22)
%24 = load i64, i64* %2
%25 = load i64, i64* %5
%26 = sub nsw i64 %24, %25
%27 = call i64 @mul(i64 %26)
%28 = load i64, i64* %5
%29 = call i64 @mul(i64 %28)
%30 = mul nsw i64 %29, %27
%31 = sdiv i64 %23, %30
%32 = load i64, i64* %3
%33 = srem i64 %31, %32
%34 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %33)
%35 = add nuw nsw i64 %20, 1
%36 = load i64, i64* %4
%37 = icmp slt i64 %35, %36
br i1 %37, label %19, label %38
38: ; preds = %19, %14
%39 = add nuw nsw i64 %15, 1
%40 = load i64, i64* %1
%41 = icmp slt i64 %39, %40
br i1 %41, label %14, label %42
42: ; preds = %38, %0
ret i32 0
}