#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 /*<<< orphan*/ scanf(char *, long long *, ...);
int main(void) {
  long long int c = 0, t, n = 0, r, g, b, k;
  scanf("%lld", &t);
  while (t--) {
    c = 0;
    n = 0;
    scanf("%lld%lld%lld", &r, &g, &b);
    scanf("%lld", &k);
    if (k == 1)
      printf("1\n");
    else {
      if (r - k >= 0)
        c += k - 1;
      else
        c += r;
      if (g - k >= 0)
        c += k - 1;
      else
        c += g;
      if (b - k >= 0)
        c += k - 1;
      else
        c += b;
      printf("%lld\n", c + 1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xec2(%rip),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
je     1278 <main+0x158>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe7f(%rip),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
mov    $0x0,%al
call   1040 <scanf@plt>
lea    0xe6d(%rip),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
cmpq   $0x1,-0x40(%rbp)
jne    11c4 <main+0xa4>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1273 <main+0x153>
mov    -0x28(%rbp),%rax
sub    -0x40(%rbp),%rax
cmp    $0x0,%rax
jl     11eb <main+0xcb>
mov    -0x40(%rbp),%rax
sub    $0x1,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    11f7 <main+0xd7>
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x40(%rbp),%rax
cmp    $0x0,%rax
jl     121e <main+0xfe>
mov    -0x40(%rbp),%rax
sub    $0x1,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    122a <main+0x10a>
mov    -0x30(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x38(%rbp),%rax
sub    -0x40(%rbp),%rax
cmp    $0x0,%rax
jl     1251 <main+0x131>
mov    -0x40(%rbp),%rax
sub    $0x1,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    125d <main+0x13d>
mov    -0x38(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1151 <main+0x31>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xec3(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
subq   $0x1,(%rsp)
jb     1205 <main+0xd5>
lea    0xe9f(%rip),%r14
lea    0x20(%rsp),%r15
lea    0x18(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xe91(%rip),%rbx
lea    0x8(%rsp),%rbp
jmp    11c7 <main+0x97>
xchg   %ax,%ax
mov    0x20(%rsp),%rcx
lea    -0x1(%rax),%rdx
cmp    %rax,%rcx
cmovge %rdx,%rcx
mov    0x18(%rsp),%rsi
cmp    %rax,%rsi
cmovge %rdx,%rsi
mov    0x10(%rsp),%rdi
cmp    %rax,%rdi
cmovl  %rdi,%rdx
add    %rcx,%rsi
add    %rdx,%rsi
add    $0x1,%rsi
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
subq   $0x1,(%rsp)
jb     1205 <main+0xd5>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <scanf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x8(%rsp),%rax
cmp    $0x1,%rax
jne    1180 <main+0x50>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
subq   $0x1,(%rsp)
jae    11c7 <main+0x97>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [13 x i8] c"%lld%lld%lld\00"
@.str.2 = [3 x i8] c"1\0A\00"
@.str.3 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 0, i32* %1
store i64 0, i64* %2
store i64 0, i64* %4
%9 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %3)
br label %10
10: ; preds = %67, %0
%11 = load i64, i64* %3
%12 = add nsw i64 %11, -1
store i64 %12, i64* %3
%13 = icmp ne i64 %11, 0
br i1 %13, label %14, label %68
14: ; preds = %10
store i64 0, i64* %2
store i64 0, i64* %4
%15 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64* %5, i64* %6, i64* %7)
%16 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %8)
%17 = load i64, i64* %8
%18 = icmp eq i64 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %67
21: ; preds = %14
%22 = load i64, i64* %5
%23 = load i64, i64* %8
%24 = sub nsw i64 %22, %23
%25 = icmp sge i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = load i64, i64* %8
%28 = sub nsw i64 %27, 1
%29 = load i64, i64* %2
%30 = add nsw i64 %29, %28
store i64 %30, i64* %2
br label %35
31: ; preds = %21
%32 = load i64, i64* %5
%33 = load i64, i64* %2
%34 = add nsw i64 %33, %32
store i64 %34, i64* %2
br label %35
35: ; preds = %31, %26
%36 = load i64, i64* %6
%37 = load i64, i64* %8
%38 = sub nsw i64 %36, %37
%39 = icmp sge i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i64, i64* %8
%42 = sub nsw i64 %41, 1
%43 = load i64, i64* %2
%44 = add nsw i64 %43, %42
store i64 %44, i64* %2
br label %49
45: ; preds = %35
%46 = load i64, i64* %6
%47 = load i64, i64* %2
%48 = add nsw i64 %47, %46
store i64 %48, i64* %2
br label %49
49: ; preds = %45, %40
%50 = load i64, i64* %7
%51 = load i64, i64* %8
%52 = sub nsw i64 %50, %51
%53 = icmp sge i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %49
%55 = load i64, i64* %8
%56 = sub nsw i64 %55, 1
%57 = load i64, i64* %2
%58 = add nsw i64 %57, %56
store i64 %58, i64* %2
br label %63
59: ; preds = %49
%60 = load i64, i64* %7
%61 = load i64, i64* %2
%62 = add nsw i64 %61, %60
store i64 %62, i64* %2
br label %63
63: ; preds = %59, %54
%64 = load i64, i64* %2
%65 = add nsw i64 %64, 1
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 %65)
br label %67
67: ; preds = %63, %19
br label %10
68: ; preds = %10
ret i32 0
}

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