#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(void) {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    scanf("%d%d", &n, &k);
    int i, max = 0, c;
    for (i = 2; i <= k; ++i) {
      c = n % i;
      if (c >= max) {
        max = c;
      }
    }
    printf("%d\n", max);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xecc(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     11c8 <main+0xa8>
lea    0xea4(%rip),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x2,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     11b4 <main+0x94>
mov    -0xc(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     11a1 <main+0x81>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11a6 <main+0x86>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1179 <main+0x59>
mov    -0x18(%rbp),%esi
lea    0xe47(%rip),%rdi
call   1030 <printf@plt>
jmp    1141 <main+0x21>
xor    %eax,%eax
add    $0x20,%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
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
subl   $0x1,0xc(%rsp)
jae    1159 <main+0x39>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xea0(%rip),%r14
lea    0x14(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xe94(%rip),%r13
jmp    1191 <main+0x71>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
subl   $0x1,0xc(%rsp)
jb     1148 <main+0x28>
xor    %ebp,%ebp
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x10(%rsp),%eax
cmp    $0x2,%eax
jl     1180 <main+0x60>
mov    0x14(%rsp),%ecx
lea    -0x1(%rax),%r8d
cmp    $0x2,%eax
jne    11d0 <main+0xb0>
xor    %ebp,%ebp
mov    $0x2,%edi
jmp    1200 <main+0xe0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r8d,%esi
and    $0xfffffffe,%esi
xor    %ebp,%ebp
mov    $0x2,%edi
nopl   (%rax)
mov    %edi,%ebx
mov    %ecx,%eax
cltd
idiv   %edi
cmp    %ebp,%edx
cmovge %edx,%ebp
lea    0x1(%rbx),%edi
mov    %ecx,%eax
cltd
idiv   %edi
cmp    %ebp,%edx
cmovge %edx,%ebp
lea    0x2(%rbx),%edi
cmp    %esi,%ebx
jne    11e0 <main+0xc0>
test   $0x1,%r8b
je     1180 <main+0x60>
mov    %ecx,%eax
cltd
idiv   %edi
cmp    %ebp,%edx
cmovge %edx,%ebp
jmp    1180 <main+0x60>

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* %2)
br label %9
9: ; preds = %32, %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 %35
13: ; preds = %9
%14 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %3, i32* %4)
store i32 0, i32* %6
store i32 2, i32* %5
br label %15
15: ; preds = %29, %13
%16 = load i32, i32* %5
%17 = load i32, i32* %4
%18 = icmp sle i32 %16, %17
br i1 %18, label %19, label %32
19: ; preds = %15
%20 = load i32, i32* %3
%21 = load i32, i32* %5
%22 = srem i32 %20, %21
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = icmp sge i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = load i32, i32* %7
store i32 %27, i32* %6
br label %28
28: ; preds = %26, %19
br label %29
29: ; preds = %28
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %15
32: ; preds = %15
%33 = load i32, i32* %6
%34 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %9
35: ; 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 = add nsw i32 %6, -1
store i32 %7, i32* %1
%8 = icmp eq i32 %6, 0
br i1 %8, label %52, label %9
9: ; preds = %0
%10 = bitcast i32* %2 to i8*
%11 = bitcast i32* %3 to i8*
br label %12
12: ; preds = %9, %46
%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, 2
br i1 %15, label %46, label %16
16: ; preds = %12
%17 = load i32, i32* %2
%18 = add i32 %14, -1
%19 = and i32 %18, 1
%20 = icmp eq i32 %14, 2
br i1 %20, label %37, label %21
21: ; preds = %16
%22 = and i32 %18, -2
br label %23
23: ; preds = %23, %21
%24 = phi i32 [ 2, %21 ], [ %34, %23 ]
%25 = phi i32 [ 0, %21 ], [ %33, %23 ]
%26 = phi i32 [ 0, %21 ], [ %35, %23 ]
%27 = srem i32 %17, %24
%28 = icmp slt i32 %27, %25
%29 = select i1 %28, i32 %25, i32 %27
%30 = or i32 %24, 1
%31 = srem i32 %17, %30
%32 = icmp slt i32 %31, %29
%33 = select i1 %32, i32 %29, i32 %31
%34 = add nuw i32 %24, 2
%35 = add i32 %26, 2
%36 = icmp eq i32 %35, %22
br i1 %36, label %37, label %23
37: ; preds = %23, %16
%38 = phi i32 [ undef, %16 ], [ %33, %23 ]
%39 = phi i32 [ 2, %16 ], [ %34, %23 ]
%40 = phi i32 [ 0, %16 ], [ %33, %23 ]
%41 = icmp eq i32 %19, 0
br i1 %41, label %46, label %42
42: ; preds = %37
%43 = srem i32 %17, %39
%44 = icmp slt i32 %43, %40
%45 = select i1 %44, i32 %40, i32 %43
br label %46
46: ; preds = %42, %37, %12
%47 = phi i32 [ 0, %12 ], [ %38, %37 ], [ %45, %42 ]
%48 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %1
%50 = add nsw i32 %49, -1
store i32 %50, i32* %1
%51 = icmp eq i32 %49, 0
br i1 %51, label %52, label %12
52: ; preds = %46, %0
ret i32 0
}