#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 */
unsigned long long calculateFibo(unsigned long long);
unsigned long long gcd(unsigned long long, unsigned long long);
int /*<<< orphan*/ printf(char *, unsigned long long, ...);
int /*<<< orphan*/ scanf(char *, ...);
int main() {
  unsigned long test = 0, a, b, c;
  unsigned long long N = 0, M = 0, num_1, num_2, GCD;
  unsigned long long fibo1, fib02;
  scanf("%lu", &test);
  while (test != 0) {
    scanf("%llu", &N);
    fibo1 = calculateFibo(N);
    fib02 = calculateFibo(N + 1);
    printf("Fibo Values: %llu %llu", fibo1, fib02);
    scanf("%lu", &a);
    scanf("%lu", &b);
    scanf("%lu", &c);
    num_1 = a * fibo1 + b * fib02 + c * (fib02 + fibo1);
    scanf("%lu", &a);
    scanf("%lu", &b);
    scanf("%lu", &c);
    num_2 = a * fibo1 + b * fib02 + c * (fib02 + fibo1);
    scanf("%llu", &M);
    GCD = gcd(num_1, num_2);
    printf("%llu", GCD % M);
    --test;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    0xe92(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
cmpq   $0x0,-0x10(%rbp)
je     12df <main+0x19f>
lea    0xe8b(%rip),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <calculateFibo@plt>
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1030 <calculateFibo@plt>
mov    %rax,-0x60(%rbp)
mov    -0x58(%rbp),%rsi
mov    -0x60(%rbp),%rdx
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2f(%rip),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
lea    0xe1d(%rip),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
lea    0xe0b(%rip),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x18(%rbp),%rax
imul   -0x58(%rbp),%rax
mov    -0x20(%rbp),%rcx
imul   -0x60(%rbp),%rcx
add    %rcx,%rax
mov    -0x28(%rbp),%rcx
mov    -0x60(%rbp),%rdx
add    -0x58(%rbp),%rdx
imul   %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
lea    0xdcd(%rip),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
lea    0xdbb(%rip),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
lea    0xda9(%rip),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x18(%rbp),%rax
imul   -0x58(%rbp),%rax
mov    -0x20(%rbp),%rcx
imul   -0x60(%rbp),%rcx
add    %rcx,%rax
mov    -0x28(%rbp),%rcx
mov    -0x60(%rbp),%rdx
add    -0x58(%rbp),%rdx
imul   %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
lea    0xd81(%rip),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1060 <gcd@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x38(%rbp)
mov    %rdx,%rsi
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1179 <main+0x39>
mov    -0x4(%rbp),%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
movq   $0x0,0x20(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x28(%rsp)
lea    0xe90(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
cmpq   $0x0,0x20(%rsp)
je     12df <main+0x19f>
lea    0xe87(%rip),%rbx
jmp    11be <main+0x7e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edx,%edx
div    %rcx
mov    %rdx,%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
addq   $0xffffffffffffffff,0x20(%rsp)
je     12df <main+0x19f>
mov    %rbx,%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x8(%rsp),%rdi
call   1030 <calculateFibo@plt>
mov    %rax,%r15
mov    0x8(%rsp),%rdi
add    $0x1,%rdi
call   1030 <calculateFibo@plt>
mov    %rax,%r13
lea    0xe12(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdfa(%rip),%rbx
mov    %rbx,%rdi
lea    0x30(%rsp),%r12
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    %rbx,%rdi
lea    0x18(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    %rbx,%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x30(%rsp),%rax
imul   %r15,%rax
mov    0x18(%rsp),%rcx
imul   %r13,%rcx
add    %rax,%rcx
lea    (%r15,%r13,1),%rbp
mov    0x10(%rsp),%r14
imul   %rbp,%r14
add    %rcx,%r14
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    %rbx,%rdi
lea    0x18(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    %rbx,%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
imul   0x30(%rsp),%r15
imul   0x18(%rsp),%r13
add    %r15,%r13
lea    0xd7a(%rip),%rbx
imul   0x10(%rsp),%rbp
add    %r13,%rbp
mov    %rbx,%rdi
lea    0x28(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1060 <gcd@plt>
mov    0x28(%rsp),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
jne    11a0 <main+0x60>
xor    %edx,%edx
div    %ecx
mov    %edx,%esi
jmp    11a8 <main+0x68>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%lu\00"
@.str.1 = [5 x i8] c"%llu\00"
@.str.2 = [23 x i8] c"Fibo Values: %llu %llu\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
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i32 0, i32* %1
store i64 0, i64* %2
store i64 0, i64* %6
store i64 0, i64* %7
%13 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %2)
br label %14
14: ; preds = %17, %0
%15 = load i64, i64* %2
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %69
17: ; preds = %14
%18 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %6)
%19 = load i64, i64* %6
%20 = call i64 @calculateFibo(i64 %19)
store i64 %20, i64* %11
%21 = load i64, i64* %6
%22 = add i64 %21, 1
%23 = call i64 @calculateFibo(i64 %22)
store i64 %23, i64* %12
%24 = load i64, i64* %11
%25 = load i64, i64* %12
%26 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i64 %24, i64 %25)
%27 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %3)
%28 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %4)
%29 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %5)
%30 = load i64, i64* %3
%31 = load i64, i64* %11
%32 = mul i64 %30, %31
%33 = load i64, i64* %4
%34 = load i64, i64* %12
%35 = mul i64 %33, %34
%36 = add i64 %32, %35
%37 = load i64, i64* %5
%38 = load i64, i64* %12
%39 = load i64, i64* %11
%40 = add i64 %38, %39
%41 = mul i64 %37, %40
%42 = add i64 %36, %41
store i64 %42, i64* %8
%43 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %3)
%44 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %4)
%45 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %5)
%46 = load i64, i64* %3
%47 = load i64, i64* %11
%48 = mul i64 %46, %47
%49 = load i64, i64* %4
%50 = load i64, i64* %12
%51 = mul i64 %49, %50
%52 = add i64 %48, %51
%53 = load i64, i64* %5
%54 = load i64, i64* %12
%55 = load i64, i64* %11
%56 = add i64 %54, %55
%57 = mul i64 %53, %56
%58 = add i64 %52, %57
store i64 %58, i64* %9
%59 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %7)
%60 = load i64, i64* %8
%61 = load i64, i64* %9
%62 = call i64 @gcd(i64 %60, i64 %61)
store i64 %62, i64* %10
%63 = load i64, i64* %10
%64 = load i64, i64* %7
%65 = urem i64 %63, %64
%66 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %65)
%67 = load i64, i64* %2
%68 = add i64 %67, -1
store i64 %68, i64* %2
br label %14
69: ; preds = %14
%70 = load i32, i32* %1
ret i32 %70
}

LLVM IR (-O2):
@.str = [4 x i8] c"%lu\00"
@.str.1 = [5 x i8] c"%llu\00"
@.str.2 = [23 x i8] c"Fibo Values: %llu %llu\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%8 = bitcast i64* %2 to i8*
%9 = bitcast i64* %3 to i8*
%10 = bitcast i64* %4 to i8*
%11 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%12 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%13 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %1)
%14 = load i64, i64* %1
%15 = icmp eq i64 %14, 0
br i1 %15, label %55, label %16
16: ; preds = %0, %16
%17 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %5)
%18 = load i64, i64* %5
%19 = call i64 @calculateFibo(i64 %18)
%20 = load i64, i64* %5
%21 = add i64 %20, 1
%22 = call i64 @calculateFibo(i64 %21)
%23 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i64 %19, i64 %22)
%24 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %2)
%25 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %3)
%26 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %4)
%27 = load i64, i64* %2
%28 = mul i64 %27, %19
%29 = load i64, i64* %3
%30 = mul i64 %29, %22
%31 = add i64 %30, %28
%32 = load i64, i64* %4
%33 = add i64 %22, %19
%34 = mul i64 %32, %33
%35 = add i64 %31, %34
%36 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %2)
%37 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %3)
%38 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %4)
%39 = load i64, i64* %2
%40 = mul i64 %39, %19
%41 = load i64, i64* %3
%42 = mul i64 %41, %22
%43 = add i64 %42, %40
%44 = load i64, i64* %4
%45 = mul i64 %44, %33
%46 = add i64 %43, %45
%47 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %6)
%48 = call i64 @gcd(i64 %35, i64 %46)
%49 = load i64, i64* %6
%50 = urem i64 %48, %49
%51 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %50)
%52 = load i64, i64* %1
%53 = add i64 %52, -1
store i64 %53, i64* %1
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %16
55: ; preds = %16, %0
ret i32 0
}