Today a plane was hijacked by a maniac. All the passengers of the flight are taken as hostage. Chef is also one of them.
He invited one of the passengers to play a game with him. If he loses the game, he will release all the passengers, otherwise he will kill all of them. A high risk affair it is.

Chef volunteered for this tough task. He was blindfolded by Hijacker. Hijacker brought a big black bag from his pockets. The contents of the bag is not visible. He tells Chef that the bag contains R red, G green and B blue colored balloons.

Hijacker now asked Chef to take out some balloons from the box such that there are at least K balloons of the same color and hand him over. If the taken out balloons does not contain at least K balloons of the same color, then the hijacker will shoot everybody. Chef is very scared and wants to leave this game as soon as possible, so he will draw the minimum number of balloons so as to save the passengers. Can you please help scared Chef to find out the minimum number of balloons he should take out.

-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. 

The first line of each test case contains a three space-separated integers R, G and B. 

The second line contains only one integer K.

-----Output-----
For each test case, output a single line containing one integer - the minimum number of balloons Chef need to take out from the bag.

-----Constraints-----
- 1 ≤ T ≤ 1000
- 1 ≤ R, G, B ≤ 109
- 1 ≤ K ≤ max{R, G, B}

-----Subtasks-----
- Subtask 1 (44 points): 1 ≤ R, G, B ≤ 10
- Subtask 2 (56 points):  No additional constraints

-----Example-----
Input:
2
3 3 3
1
3 3 3
2

Output:
1
4

-----Explanation-----
Example case 2. In the worst-case scenario first three balloons will be of the three different colors and only after fourth balloon Chef will have two balloons of the same color. So, Chef might need to fetch 4 balloons

Difficulty: interview

Starter code:

Solutions:
import sys
test_cases = int(input())
for i in range(0,test_cases):
 count = input().split()
 #print count
 count_r = int(count[0])
 count_g = int(count[1])
 count_b = int(count[2])
 k = int(input())
 if k is 1:
  total = 1
 else:   
  total = 1
  if count_r < k:
   total = total + count_r
  else:
   total = total + (k-1)
  if count_g < k:
   total = total + count_g
  else:
   total = total + (k-1) 
  if count_b < k:
   total = total + count_b
  else:
   total = total + (k-1)
 
 print(total)

import sys
test_cases = int(input())
for i in range(0,test_cases):
 count = input().split()
 #print count
 count_r = int(count[0])
 count_g = int(count[1])
 count_b = int(count[2])
 k = int(input())
 if k is 1:
  total =1
 else:   
  total = 1
  if count_r < k:
   total = total + count_r
  else:
   total = total + (k-1)
  if count_g < k:
   total = total + count_g
  else:
   total = total + (k-1) 
  if count_b < k:
   total = total + count_b
  else:
   total = total + (k-1)
 
 print(total)

R=G=B=atmpt=counter=0
t=int(input())
for i in range(t):
 R,G,B=list(map(int,input().split()))
 k=int(input())
 if R<k:
  atmpt=R
 else:
  atmpt=k-1
 if G<k:
  atmpt=atmpt+G
 else:
  atmpt=atmpt+k-1
 if B<k:
  atmpt=atmpt+B
 else:
  atmpt=atmpt+k-1
 atmpt=atmpt+1
 print(atmpt)
 atmpt=0

t = eval(input())
for _ in range(t):
 r, g, b = list(map(int, input().split()))
 k = eval(input())
 d = {}
 d['R'] = r
 d['G'] = g
 d['B'] = b
 c = 0
 r = {'r':0, 'g':0, 'b':0}
 while r['r'] != k or r['g'] != k or r['b'] != k:
  if d['R'] > 0 and k > 0:
   d['R'] -= 1
   r['r'] += 1
   c += 1
   if r['r'] == k or r['g'] == k or r['b'] == k:
    break
  if d['G'] > 0 and k > 0:
   d['G'] -= 1
   r['g'] += 1
   c += 1
   if r['r'] == k or r['g'] == k or r['b'] == k:
    break
  if d['B'] > 0 and k > 0:
   d['B'] -= 1
   r['b'] += 1
   c += 1
   if r['r'] == k or r['g'] == k or r['b'] == k:
    break
 print(c)

t=int(input())
while t>0:
 t-=1
 arr=list(map(int,input().split()))
 k=int(input())
 arr.sort()
 b=arr[0]
 g=arr[1]
 r=arr[2]
 if k<=b:
  print((k-1)*3+1)
 elif k<=g:
  print(3*b+(k-b-1)*2+1)
 elif k<=r:
  print(2*g+b+k-g)


t=input()
t=int(t)
while t>0:
 inp=input()
 values=inp.split()
 
 k=input()
 k=int(k)
 
 
 lst1 = [int(values[0]), int(values[1]), int(values[2])]
 lst = sorted(lst1)
 
 
  
 
 if lst[0]>=k:
  num=((k-1)*3)+1
  print(num)
 elif lst[1]>=k:
  num= (lst[0]*3)+((k-lst[0]-1)*2)+1
  print(num)
 elif lst[2]>=k:
  num= (lst[0]*3)+((lst[1]-lst[0])*2)+(k-lst[1])
  print(num)
 
 
 t=t-1

for i in range(eval(input())):
 l=list(map(int,input().split()))
 l.sort()
 b=set(l)
 k1=eval(input())
 answer=0
 if (k1 <= (l[0] + 1)):
  answer += 1 + (3 * (k1 - 1))
  print(answer)
 elif (k1 <= (l[1] + 1)):
  answer += 1 + (3 * l[0])
  k1 -= l[0]
  answer += 2 * (k1 - 1)
  print(answer)
 else:
  answer += 1 + (3 * l[0])
  k1 -= l[0]
  answer += 2 * (l[1] - l[0])
  k1 -= (l[1] - l[0])
  answer += k1 - 1
  print(answer)

T = eval(input())

for _ in range(T):
 col = sorted(map(int, input().split()))
 k = eval(input())
 ans = min(col[0], k-1) + min(col[1], k-1) + min(col[2], k-1) + 1
 print(ans)

t = int(input())
while t:
 t-=1
 r,g,b,k,ans,cnt=0,0,0,0,0,0;
 r,g,b= input().split()
 r,g,b= int(r),int(g),int(b)
 k = int(input())
 if(r<k): ans+=r;
 else: cnt+=1;
 if(g<k): ans+=g;
 else: cnt+=1;
 if(b<k): ans+=b;
 else: cnt+=1;
 ans += cnt*(k-1) +1;
 print(ans)
 
 


__author__ = 'Deepak'
t=eval(input())
while t>0:
 t-=1
 r,g,b= list(map(int,input().split()))
 k=eval(input())
 a = [r,g,b]
 a.sort()
 if k <=a[0]:
  print(k*3-2)
 elif k > a[0] and k <=a[1]:
  print(a[0]*3+(k-a[0])*2-1)
 else:
  print(a[0]*3+(a[1]-a[0])*2+ k-a[1])




for t in range(eval(input())):
 arr=list(map(int,input().split()))
 k=eval(input())
 m=-1
 if k<=arr[0]: m=0
 elif k<=arr[1]: m=1
 elif k<=arr[2]: m=2
 if k==1: print(1)
 else: print(min(k,arr[m%3])+min(k-1,arr[(m+1)%3])+min(k-1,arr[(m+2)%3]))

import sys
test_cases = int(input())
for i in range(0,test_cases):
 count = input().split()
 count_r = int(count[0])
 count_g = int(count[1])
 count_b = int(count[2])
 k = int(input())
 if k is 1:
  print(1)
 else:   
  total = 0
  for i in range (1,k):
   if count_r != 0:
    count_r = count_r -1 
    total =total+1 
   if count_g != 0:
    count_g = count_g -1 
    total =total+1 
   if count_b != 0:
    count_b = count_b -1 
    total =total+1 
  total =total + 1
  print(total)


import sys
test_cases = int(input())
for i in range(0,test_cases):
 count = input().split()
 count_r = int(count[0])
 count_g = int(count[1])
 count_b = int(count[2])
 k = int(input())
 if k is 1:
  print(1)
 else:   
  total = 0
  for i in range (1,k):
   if count_r is not 0:
    count_r = count_r -1 
    total =total+1 
   if count_g is not 0:
    count_g = count_g -1 
    total =total+1 
   if count_b is not 0:
    count_b = count_b -1 
    total =total+1 
  total =total + 1
  print(total)


import sys
test_cases = int(input())
for i in range(0,test_cases):
 count_r,count_g,count_b = input().split()
 count_r = int(count_r)
 count_g = int(count_g)
 count_b = int(count_b)
 k = int(input())
 if k is 1:
  print(1)
 else:   
  total = 0
  for i in range (1,k):
   if count_r is not 0:
    count_r = count_r -1 
    total =total+1 
   if count_g is not 0:
    count_g = count_g -1 
    total =total+1 
   if count_b is not 0:
    count_b = count_b -1 
    total =total+1 
  total =total + 1
  print(total)


for i in range(eval(input())):
 a=list(map(int,input().split()))
 a.sort()
 b=set(a)
 k=eval(input())
 count=len(b)
 if count==1:
  ans=1+3*(k-1)
  print(ans)
 if count==2:
  if k<=a[0]+1:
   ans=1+3*(k-1)
   print(ans)
  else:
   ans=a[0]+a[1]+a[2]-1-((a[2]-k)*2)
   print(ans)
 if count==3:
  if k<=a[0]+1:
   ans=1+3*(k-1)
   print(ans)
  elif k<=a[1]+1:
   ans=1+3*(a[0])+2*(k-(a[0]+1))
   print(ans)
  else:
   ans=k+a[0]+a[1]
   print(ans)




for i in range(eval(input())):
 a=list(map(int,input().split()))
 a.sort()
 b=set(a)
 k=eval(input())
 count=len(b)
 if count==1:
  ans=1+3*(k-1)
  print(ans)
 if count==2:
  if k<=a[0]+1:
   ans=1+3*(k-1)
   print(ans)
  else:
   ans=a[0]+a[1]+a[2]-1-((a[2]-k)*2)
   print(ans)
 if count==3:
  if k<=a[0]+1:
   ans=1+3*(k-1)
   print(ans)
  elif k<=a[1]+1:
   ans=1+3*(a[0])+2*(k-(a[0]+1))
   print(ans)
  else:
   ans=k+a[0]+a[1]
   print(ans)




for i in range(eval(input())):
 a=list(map(int,input().split()))
 a.sort()
 b=set(a)
 k=eval(input())
 count=len(b)
 if count==1:
  ans=1+3*(k-1)
  print(ans)
 if count==2:
  if k<=a[0]+1:
   ans=1+3*(k-1)
   print(ans)
  else:
   ans=a[0]+a[1]+a[2]-1-((a[2]-k)*2)
   print(ans)
 if count==3:
  if k<=a[0]+1:
   ans=1+3*(k-1)
   print(ans)
  elif k<=a[1]+1:
   ans=1+3*(a[0])+2*(k-(a[0]+1))
   print(ans)
  else:
   ans=k+a[0]+a[1]
   print(ans)




T = int(input())

def solve(nums, K):
 if K <= 0 or len(nums) == 0:
  return 0
 c = [x for x in nums if x > 0]
 c.sort()
 #print c
 #print K
 if c[0] >= K:
  return (K-1)*len(c)+1
 else:
  a = c[0]
  return len(c)*a + solve([x-a for x in c], K-a)

for _ in range(T):
 [R, G, B] = [int(x) for x in input().rstrip().split(" ")]
 K = int(input())
 print(solve([R, G, B], K))