#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 *, long long);
int /*<<< orphan*/ scanf(char *, ...);
int main() {
  int t;
  long long int diff, N, i, sum, rep, s, count;
  scanf("%d", &t);
  while (t--) {
    count = 0;
    scanf("%lld %lld", &N, &s);
    diff = s - N;
    for (i = 0; i < N; i++) {
      if ((diff - i) >= 0)
        diff -= i;
      else
        break;
    }
    printf("%lld\n", N - i);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%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     11ef <main+0xcf>
movq   $0x0,-0x40(%rbp)
lea    0xe9f(%rip),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x38(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    11d6 <main+0xb6>
mov    -0x10(%rbp),%rax
sub    -0x20(%rbp),%rax
cmp    $0x0,%rax
jl     11bb <main+0x9b>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11c0 <main+0xa0>
jmp    11d6 <main+0xb6>
jmp    11c5 <main+0xa5>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1187 <main+0x67>
mov    -0x18(%rbp),%rsi
sub    -0x20(%rbp),%rsi
lea    0xe28(%rip),%rdi
call   1030 <printf@plt>
jmp    1141 <main+0x21>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xece(%rip),%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
subl   $0x1,0x4(%rsp)
jae    1153 <main+0x33>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xea9(%rip),%r14
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
lea    0xea2(%rip),%rbx
jmp    1184 <main+0x64>
nopl   (%rax)
xor    %eax,%eax
sub    %rax,%rsi
mov    %rbx,%rdi
call   1030 <printf@plt>
subl   $0x1,0x4(%rsp)
jb     1145 <main+0x25>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x10(%rsp),%rsi
test   %rsi,%rsi
jle    1170 <main+0x50>
mov    0x8(%rsp),%rcx
sub    %rsi,%rcx
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
sub    %rax,%rcx
js     1172 <main+0x52>
add    $0x1,%rax
cmp    %rax,%rsi
jne    11b0 <main+0x90>
mov    %rsi,%rax
jmp    1172 <main+0x52>

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [10 x i8] c"%lld %lld\00"
@.str.2 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i32* %1 to i8*
%5 = bitcast i64* %2 to i8*
%6 = bitcast i64* %3 to i8*
%7 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%8 = load i32, i32* %1
%9 = add nsw i32 %8, -1
store i32 %9, i32* %1
%10 = icmp eq i32 %8, 0
br i1 %10, label %33, label %11
11: ; preds = %0, %26
%12 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64* %2, i64* %3)
%13 = load i64, i64* %2
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %26
15: ; preds = %11
%16 = load i64, i64* %3
%17 = sub nsw i64 %16, %13
br label %18
18: ; preds = %15, %23
%19 = phi i64 [ %21, %23 ], [ %17, %15 ]
%20 = phi i64 [ %24, %23 ], [ 0, %15 ]
%21 = sub nsw i64 %19, %20
%22 = icmp sgt i64 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = add nuw nsw i64 %20, 1
%25 = icmp eq i64 %24, %13
br i1 %25, label %26, label %18
26: ; preds = %23, %18, %11
%27 = phi i64 [ 0, %11 ], [ %20, %18 ], [ %13, %23 ]
%28 = sub nsw i64 %13, %27
%29 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %28)
%30 = load i32, i32* %1
%31 = add nsw i32 %30, -1
store i32 %31, i32* %1
%32 = icmp eq i32 %30, 0
br i1 %32, label %33, label %11
33: ; preds = %26, %0
ret i32 0
}