#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 /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main() {
  int tc, T;
  scanf("%d", &T);
  for (tc = 0; tc < T; tc++) {
    int n, k;
    int i;
    int rem = 0;
    scanf("%d%d", &n, &k);
    for (i = 1; i <= k; i++) {
      rem = rem > n % i ? rem : n % i;
    }
    printf("%d\n", rem);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xecc(%rip),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e2 <main+0xc2>
movl   $0x0,-0x1c(%rbp)
lea    0xe9e(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     11c5 <main+0xa5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    -0x20(%rbp),%eax
cmp    %edx,%eax
jle    11a7 <main+0x87>
mov    -0x1c(%rbp),%edx
mov    %edx,-0x24(%rbp)
jmp    11b1 <main+0x91>
mov    -0x10(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    %edx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1178 <main+0x58>
mov    -0x1c(%rbp),%esi
lea    0xe36(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1148 <main+0x28>
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    0xecd(%rip),%rdi
xor    %r14d,%r14d
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
jle    120a <main+0xea>
lea    0xeaa(%rip),%rbx
lea    0x14(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xe9e(%rip),%r13
jmp    1189 <main+0x69>
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
add    $0x1,%r14d
cmp    0xc(%rsp),%r14d
jge    120a <main+0xea>
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x10(%rsp),%r8d
test   %r8d,%r8d
jle    1170 <main+0x50>
mov    0x14(%rsp),%ecx
cmp    $0x1,%r8d
jne    11c0 <main+0xa0>
mov    $0x1,%edi
xor    %ebp,%ebp
test   $0x1,%r8b
je     1170 <main+0x50>
jmp    11fb <main+0xdb>
xchg   %ax,%ax
mov    %rbx,%r9
mov    %r8d,%esi
and    $0xfffffffe,%esi
mov    $0x1,%edi
xor    %ebp,%ebp
mov    %ecx,%eax
cltd
idiv   %edi
cmp    %edx,%ebp
cmovle %edx,%ebp
lea    0x1(%rdi),%ebx
mov    %ecx,%eax
cltd
idiv   %ebx
cmp    %edx,%ebp
cmovle %edx,%ebp
add    $0x2,%edi
cmp    %esi,%ebx
jne    11d0 <main+0xb0>
mov    %r9,%rbx
test   $0x1,%r8b
je     1170 <main+0x50>
mov    %ecx,%eax
cltd
idiv   %edi
cmp    %edx,%ebp
cmovle %edx,%ebp
jmp    1170 <main+0x50>
xor    %eax,%eax
add    $0x18,%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 = [5 x i8] c"%d%d\00"
@.str.2 = [4 x i8] c"%d\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
store i32 0, i32* %1
%8 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
store i32 0, i32* %2
br label %9
9: ; preds = %39, %0
%10 = load i32, i32* %2
%11 = load i32, i32* %3
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %42
13: ; preds = %9
store i32 0, i32* %7
%14 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %4, i32* %5)
store i32 1, i32* %6
br label %15
15: ; preds = %33, %13
%16 = load i32, i32* %6
%17 = load i32, i32* %5
%18 = icmp sle i32 %16, %17
br i1 %18, label %19, label %36
19: ; preds = %15
%20 = load i32, i32* %7
%21 = load i32, i32* %4
%22 = load i32, i32* %6
%23 = srem i32 %21, %22
%24 = icmp sgt i32 %20, %23
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = load i32, i32* %7
br label %31
27: ; preds = %19
%28 = load i32, i32* %4
%29 = load i32, i32* %6
%30 = srem i32 %28, %29
br label %31
31: ; preds = %27, %25
%32 = phi i32 [ %26, %25 ], [ %30, %27 ]
store i32 %32, i32* %7
br label %33
33: ; preds = %31
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %15
36: ; preds = %15
%37 = load i32, i32* %7
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %36
%40 = load i32, i32* %2
%41 = add nsw i32 %40, 1
store i32 %41, i32* %2
br label %9
42: ; preds = %9
ret i32 0
}

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