#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 */
int gcd(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, ...);
int strlen(char *);
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    char arr[250];
    int a;
    scanf("%d", &a);
    scanf("%s", arr);
    if (a == 0)
      printf("%s\n", arr);
    else {
      int i, m = 0;
      for (i = 0; i < strlen(arr); ++i) {
        m = (m * 10 + arr[i] - '0') % a;
      }
      int n = gcd(a, m);
      printf("%d\n", n);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     1271 <main+0x131>
lea    0xe81(%rip),%rdi
lea    -0x114(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
lea    -0x110(%rbp),%rsi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1050 <scanf@plt>
cmpl   $0x0,-0x114(%rbp)
jne    11c9 <main+0x89>
lea    -0x110(%rbp),%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    126c <main+0x12c>
movl   $0x0,-0x11c(%rbp)
movl   $0x0,-0x118(%rbp)
mov    -0x118(%rbp),%eax
mov    %eax,-0x124(%rbp)
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x124(%rbp),%eax
cmp    %ecx,%eax
jge    1241 <main+0x101>
imul   $0xa,-0x11c(%rbp),%eax
movslq -0x118(%rbp),%rcx
movsbl -0x110(%rbp,%rcx,1),%ecx
ff
add    %ecx,%eax
sub    $0x30,%eax
cltd
idivl  -0x114(%rbp)
mov    %edx,-0x11c(%rbp)
mov    -0x118(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x118(%rbp)
jmp    11dd <main+0x9d>
mov    -0x114(%rbp),%edi
mov    -0x11c(%rbp),%esi
call   1060 <gcd@plt>
mov    %eax,-0x120(%rbp)
mov    -0x120(%rbp),%esi
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1164 <main+0x24>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0xe98(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
subl   $0x1,0xc(%rsp)
jb     1248 <main+0xf8>
lea    0xe7a(%rip),%r14
lea    0x8(%rsp),%r15
lea    0xe71(%rip),%r12
lea    0x10(%rsp),%rbx
jmp    11b3 <main+0x63>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <puts@plt>
subl   $0x1,0xc(%rsp)
jb     1248 <main+0xf8>
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmpl   $0x0,0x8(%rsp)
je     11a0 <main+0x50>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1220 <main+0xd0>
xor    %r13d,%r13d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x0(%rbp,%rbp,4),%eax
movsbl 0x10(%rsp,%r13,1),%ecx
lea    (%rcx,%rax,2),%eax
add    $0xffffffd0,%eax
cltd
idivl  0x8(%rsp)
add    $0x1,%r13
mov    %edx,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rax,%r13
jl     11f0 <main+0xa0>
jmp    1222 <main+0xd2>
nopl   0x0(%rax)
xor    %ebp,%ebp
mov    0x8(%rsp),%edi
mov    %ebp,%esi
call   1070 <gcd@plt>
lea    0xdd2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
subl   $0x1,0xc(%rsp)
jae    11b3 <main+0x63>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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