#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 mod(long);
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ scanf(char *, ...);
int sqrt(long);
int main() {
  int T;
  long int n, i, min;
  scanf("%d", &T);
  while (T--) {
    min = 100000000;
    scanf("%ld", &n);
    for (i = 1; i < sqrt(n) + 1; i++) {
      if (n % i == 0 && mod((n / i) - i) < min)
        min = mod((n / i) - i);
    }
    printf("%ld\n", min);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%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     1236 <main+0xf6>
movq   $0x5f5e100,-0x20(%rbp)
05
lea    0xe7f(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
movq   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <sqrt@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jge    1221 <main+0xe1>
mov    -0x10(%rbp),%rax
cqto
idivq  -0x18(%rbp)
cmp    $0x0,%rdx
jne    120b <main+0xcb>
mov    -0x10(%rbp),%rax
cqto
idivq  -0x18(%rbp)
mov    %rax,%rdi
sub    -0x18(%rbp),%rdi
call   1030 <mod@plt>
cmp    -0x20(%rbp),%rax
jge    120b <main+0xcb>
mov    -0x10(%rbp),%rax
cqto
idivq  -0x18(%rbp)
mov    %rax,%rdi
sub    -0x18(%rbp),%rdi
call   1030 <mod@plt>
mov    %rax,-0x20(%rbp)
jmp    1210 <main+0xd0>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1197 <main+0x57>
mov    -0x20(%rbp),%rsi
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
jmp    1161 <main+0x21>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xeab(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
subl   $0x1,0xc(%rsp)
jae    1179 <main+0x39>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe83(%rip),%r14
lea    0x10(%rsp),%r15
lea    0xe7b(%rip),%r12
jmp    11a8 <main+0x68>
xchg   %ax,%ax
mov    $0x5f5e100,%r13d
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <printf@plt>
subl   $0x1,0xc(%rsp)
jb     1168 <main+0x28>
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x10(%rsp),%rdi
call   1060 <sqrt@plt>
test   %eax,%eax
jle    1190 <main+0x50>
mov    $0x1,%ebx
mov    $0x5f5e100,%r13d
mov    $0xffffffffffffffff,%rbp
jmp    120c <main+0xcc>
cqto
idiv   %rbx
mov    %rax,%rdi
add    %rbp,%rdi
call   1030 <mod@plt>
mov    %rax,%r13
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    0x10(%rsp),%rdi
call   1060 <sqrt@plt>
add    $0x1,%eax
cltq
add    $0xffffffffffffffff,%rbp
cmp    %rax,%rbx
jge    1196 <main+0x56>
mov    0x10(%rsp),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     1230 <main+0xf0>
cqto
idiv   %rbx
test   %rdx,%rdx
jne    11f0 <main+0xb0>
jmp    1239 <main+0xf9>
nopl   0x0(%rax)
xor    %edx,%edx
div    %ebx
test   %rdx,%rdx
jne    11f0 <main+0xb0>
add    %rbp,%rax
mov    %rax,%rdi
call   1030 <mod@plt>
cmp    %r13,%rax
jge    11f0 <main+0xb0>
mov    0x10(%rsp),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
jne    11d7 <main+0x97>
xor    %edx,%edx
div    %ebx
mov    %eax,%edi
jmp    11df <main+0x9f>

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

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