#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 */
char get_char(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main() {
  int B, N, i, j, p, D;
  char str[50] = {};
  scanf("%d", &D);
  for (j = 0; j < D; j++) {
    scanf("%d %d", &B, &N);
    if (B == 0 && N == 0) {
      break;
    }
    p = 0;
    while (N > 0) {
      str[p] = get_char(N % B);
      N = N / B;
      p++;
    }
    for (i = p - 1; i >= 0; i--) {
      printf("%c", str[i]);
    }
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1040 <memset@plt>
lea    0xe9d(%rip),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1248 <main+0x108>
lea    0xe75(%rip),%rdi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
mov    $0x0,%al
call   1050 <scanf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11b3 <main+0x73>
cmpl   $0x0,-0xc(%rbp)
jne    11b3 <main+0x73>
jmp    1248 <main+0x108>
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11f4 <main+0xb4>
mov    -0xc(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %edx,%edi
call   1060 <get_char@plt>
mov    %al,%cl
movslq -0x18(%rbp),%rax
mov    %cl,-0x50(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ba <main+0x7a>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     122c <main+0xec>
movslq -0x10(%rbp),%rax
movsbl -0x50(%rbp,%rax,1),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    11fd <main+0xbd>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1178 <main+0x38>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movw   $0x0,0x40(%rsp)
lea    0xea5(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0x8(%rsp)
jle    123c <main+0x10c>
lea    0xe84(%rip),%r14
lea    0xc(%rsp),%r15
lea    0x4(%rsp),%r12
xor    %r13d,%r13d
jmp    11a9 <main+0x79>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%r13d
cmp    0x8(%rsp),%r13d
jge    123c <main+0x10c>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0xc(%rsp),%ecx
mov    0x4(%rsp),%eax
mov    %ecx,%edx
or     %eax,%edx
je     123c <main+0x10c>
test   %eax,%eax
jle    1190 <main+0x60>
mov    $0x1,%edx
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %edx,%ebp
cltd
idiv   %ecx
mov    %edx,%edi
call   1050 <get_char@plt>
mov    %al,0x10(%rsp,%rbx,1)
mov    0x4(%rsp),%eax
mov    0xc(%rsp),%ecx
cltd
idiv   %ecx
mov    %eax,0x4(%rsp)
add    $0x1,%rbx
lea    0x1(%rbp),%edx
test   %eax,%eax
jg     11e0 <main+0xb0>
test   %ebx,%ebx
jle    1190 <main+0x60>
mov    %ebp,%ebx
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    -0x2(%rbx),%eax
movsbl 0x10(%rsp,%rax,1),%edi
call   1030 <putchar@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1220 <main+0xf0>
jmp    1190 <main+0x60>
xor    %eax,%eax
add    $0x48,%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 = [6 x i8] c"%d %d\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [50 x i8]
store i32 0, i32* %1
%9 = bitcast [50 x i8]* %8 to i8*
%10 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %7)
store i32 0, i32* %5
br label %11
11: ; preds = %58, %0
%12 = load i32, i32* %5
%13 = load i32, i32* %7
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %61
15: ; preds = %11
%16 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32* %3)
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
br label %61
23: ; preds = %19, %15
store i32 0, i32* %6
br label %24
24: ; preds = %27, %23
%25 = load i32, i32* %3
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %40
27: ; preds = %24
%28 = load i32, i32* %3
%29 = load i32, i32* %2
%30 = srem i32 %28, %29
%31 = call signext i8 @get_char(i32 %30)
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 %33
store i8 %31, i8* %34
%35 = load i32, i32* %3
%36 = load i32, i32* %2
%37 = sdiv i32 %35, %36
store i32 %37, i32* %3
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
store i32 %39, i32* %6
br label %24
40: ; preds = %24
%41 = load i32, i32* %6
%42 = sub nsw i32 %41, 1
store i32 %42, i32* %4
br label %43
43: ; preds = %53, %40
%44 = load i32, i32* %4
%45 = icmp sge i32 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %43
%47 = load i32, i32* %4
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %51)
br label %53
53: ; preds = %46
%54 = load i32, i32* %4
%55 = add nsw i32 %54, -1
store i32 %55, i32* %4
br label %43
56: ; preds = %43
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %56
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 1
store i32 %60, i32* %5
br label %11
61: ; preds = %22, %11
ret i32 0
}

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