Eve is a beginner stand-up comedian. Her first show gathered a grand total of two spectators: Alice and Bob.

Eve prepared $a_1 + a_2 + a_3 + a_4$ jokes to tell, grouped by their type:

type 1: both Alice and Bob like them;

type 2: Alice likes them, but Bob doesn't;

type 3: Bob likes them, but Alice doesn't;

type 4: neither Alice nor Bob likes them.

Initially, both spectators have their mood equal to $0$. When a spectator hears a joke he/she likes, his/her mood increases by $1$. When a spectator hears a joke he/she doesn't like, his/her mood decreases by $1$. If the mood of a spectator becomes negative (strictly below zero), he/she leaves.

When someone leaves, Eve gets sad and ends the show. If no one leaves, and Eve is out of jokes, she also ends the show.

Thus, Eve wants to arrange her jokes in such a way that the show lasts as long as possible. Help her to calculate the maximum number of jokes she can tell before the show ends.


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases.

The only line of each testcase contains four integers $a_1, a_2, a_3, a_4$ ($0 \le a_1, a_2, a_3, a_4 \le 10^8$; $a_1 + a_2 + a_3 + a_4 \ge 1$) — the number of jokes of each type Eve prepared.


-----Output-----

For each testcase, print a single integer — the maximum number of jokes Eve can tell before at least one of the spectators leaves or before she runs out of jokes.


-----Examples-----

Input
4
5 0 0 0
0 0 0 5
2 5 10 6
3 0 0 7
Output
5
1
15
7


-----Note-----

In the first testcase, Eve only has jokes of the first type. Thus, there's no order to choose. She tells all her jokes, both Alice and Bob like them. Their mood becomes $5$. The show ends after Eve runs out of jokes.

In the second testcase, Eve only has jokes of the fourth type. Thus, once again no order to choose. She tells a joke, and neither Alice, nor Bob likes it. Their mood decrease by one, becoming $-1$. They both have negative mood, thus, both leave, and the show ends.

In the third testcase, first, Eve tells both jokes of the first type. Both Alice and Bob has mood $2$. Then she can tell $2$ jokes of the third type. Alice's mood becomes $0$. Bob's mood becomes $4$. Then $4$ jokes of the second type. Alice's mood becomes $4$. Bob's mood becomes $0$. Then another $4$ jokes of the third type. Alice's mood becomes $0$. Bob's mood becomes $4$. Then the remaining joke of the second type. Alice's mood becomes $1$. Bob's mood becomes $3$. Then one more joke of the third type, and a joke of the fourth type, for example. Alice's mood becomes $-1$, she leaves, and the show ends.

In the fourth testcase, Eve should first tell the jokes both spectators like, then the jokes they don't. She can tell $4$ jokes of the fourth type until the spectators leave.

Source: codeforces | Difficulty: EASY
Tags: Mathematics

Greedy algorithms


Solutions:
for s in [*open(0)][1:]:
	(a, b, c, d) = map(int, s.split())
	print(+(a < 1) or a + 2 * min(b, c) + min(a + 1, abs(b - c) + d))


import sys
input = sys.stdin.readline

def readList():
	return list(map(int, input().split()))

def readInt():
	return int(input())

def readInts():
	return map(int, input().split())

def readStr():
	return input().strip()

def solve():
	(a1, a2, a3, a4) = readInts()
	r = abs(a2 - a3) + a4
	if not a1:
		return 1
	else:
		ans = a1 + 2 * min(a2, a3)
		return ans + r if a1 >= r else ans + a1 + 1
for _ in range(int(input())):
	print(solve())


import sys
input = sys.stdin.readline

def solve():
	(a1, a2, a3, a4) = list(map(int, input().split()))
	if a1 == 0:
		print(1)
		return
	ans = a1 + 2 * min(a2, a3)
	diff = max(a2, a3) - min(a2, a3)
	if a1 >= diff:
		a1 -= diff
		ans += diff
	elif a1 < diff:
		print(ans + a1 + 1)
		return
	if a1 < a4:
		print(ans + a1 + 1)
		return
	elif a1 >= a4:
		a1 -= a4
		ans += a4
	print(ans)
t = int(input())
for _ in range(t):
	solve()


import sys
put = sys.stdin.readline
t = int(put())
while t:
	t -= 1
	a = [0] + list(map(int, put().split()))
	if a[1]:
		a1 = a[1]
		a2 = min(a[1] + a[3], a[2])
		a3 = min(a[1] + a[2], a[3])
		a[2] -= a2
		a[3] -= a3
		if a[2] or a[3]:
			print(a1 + a2 + a3 + 1)
		else:
			Alice = a1 + a2 - a3
			Bob = a1 - a2 + a3
			a4 = min(Alice + 1, Bob + 1, a[4])
			print(a1 + a2 + a3 + a4)
	else:
		print(1)


import sys
input = sys.stdin.readline

def solve():
	(a, b, c, d) = map(int, input().split())
	counter = 0
	mood = 1
	if a == 0:
		if b or c or d:
			print(1)
		else:
			print(0)
		return
	counter += a
	mood += counter
	dif = min(b, c)
	c = max(b, c) - dif
	counter += dif * 2
	c = min(mood, c)
	counter += c
	mood -= c
	counter += min(mood, d)
	print(counter)
a = int(input().strip())
for i in range(a):
	solve()


def comedian(a, b, c, d):
	if a == 0:
		return 1
	alice = a
	bob = a
	to_ret = a
	while alice >= 0 and bob >= 0 and (b + c + d != 0):
		if b > 0 and bob != 0:
			bob -= 1
			b -= 1
			alice += 1
			to_ret += 1
		elif c > 0 and alice != 0:
			alice -= 1
			c -= 1
			bob += 1
			to_ret += 1
		else:
			alice -= 1
			bob -= 1
			d -= 1
			to_ret += 1
	return to_ret

def comedian_improved(a, b, c, d):
	if a == 0:
		return 1
	else:
		to_ret = a + 2 * min(b, c) + min(a + 1, d + abs(b - c))
	return to_ret
val = int(input())
result = [] * val
for i in range(val):
	(a, b, c, d) = map(int, input().split())
	result.append(comedian_improved(a, b, c, d))
for res in result:
	print(res)


import sys
from math import *
from collections import defaultdict, deque
input = sys.stdin.readline
mod = 10 ** 9 + 7
INF = 1061109567
N = int(2 * 10 ** 5 + 5)
LINF = 4557430888798830399

def solve():
	(a1, a2, a3, a4) = map(int, input().split())
	if a1 == 0:
		print(1)
		return
	ans = 2 * a1 + 2 * min(a2, a3) + 1
	ans = min(a1 + a2 + a3 + a4, ans)
	print(ans)
T = int(input())
for t in range(T):
	solve()


import sys, math, cmath, time
from bisect import bisect_left
start_time = time.time()

def solve(t):
	l = [int(k) for k in inps()]
	(a1, a2, a3, a4) = (l[0], l[1], l[2], l[3])
	if a1 == 0:
		print(1)
		return
	if min(a2, a3) + a1 - max(a2, a3) >= 0:
		x = a1 + 2 * min(a2, a3) + min(a1 + 1, abs(a3 - a2)) + min(a1 - a2 + a3 + 1, a1 - a3 + a2 + 1, a4)
	else:
		x = a1 + 2 * min(a2, a3) + min(a1 + 1, abs(a3 - a2))
	print(x)

def main():
	testcases = 1
	testcases = inp()
	for t in range(testcases):
		solve(t)

def inp():
	return int(input())

def inps():
	return input().split()
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


def joke_num_calc(a_1: int, a_2: int, a_3: int, a_4: int) -> int:
	if a_1 == 0:
		return 1
	else:
		return a_1 + min(a_2, a_3) * 2 + min(a_1 + 1, abs(a_2 - a_3) + a_4)
t_number = int(input())
t_dict = dict()
for i in range(t_number):
	(a_1, a_2, a_3, a_4) = map(int, input().split())
	t_dict[i] = joke_num_calc(a_1, a_2, a_3, a_4)
for i in range(t_number):
	print(t_dict[i])


from sys import stdin, stdout
from math import gcd
input = stdin.readline
for _ in range(int(input())):
	(a1, a2, a3, a4) = map(int, input().split())
	X = min(a2, a3)
	if a1 == 0 and (a2 > 0 or a3 > 0 or a4 > 0):
		print(1)
	else:
		ct = a1 + 2 * X
		am = a1
		bm = a1
		a2 = a2 - X
		a3 = a3 - X
		if a2 > 0:
			if bm < a2:
				ct += bm + 1
				am = a1 + bm
				bm = -1
			else:
				am = a1 + a2
				bm = a1 - a2
				ct += a2
		elif a3 > 0:
			if am < a3:
				ct += am + 1
				bm = a1 + am
				am = -1
			else:
				am = a1 - a3
				bm = a1 + a3
				ct += a3
		k = min(am, bm)
		if k == 0:
			print(ct + min(a4, 1))
		else:
			if a4 <= k:
				ct += a4
			else:
				ct += k + 1
			print(ct)


for s in [*open(0)][1:]:
	(a, *b, d) = map(int, s.split())
	(b, c) = sorted(b)
	print((1, a + 2 * b + min(a + 1, c - b + d))[a > 0])


import sys
input = sys.stdin.readline

def solve():
	L = [*map(int, input().split())]
	a1 = L[0]
	if a1 == 0:
		return 1
	a2 = min(L[1], L[2]) * 2
	l1 = L[1] - a2 // 2
	l2 = L[2] - a2 // 2
	if L[3] < L[0]:
		a3 = L[3]
		q = max(l1, l2)
		l = L[0] - L[3]
		if q > l:
			a3 += l + 1
		else:
			a3 += q
	elif L[3] == L[0]:
		a3 = L[3]
		if max(l1, l2):
			a3 += 1
	else:
		a3 = L[0] + 1
	return a1 + a2 + a3
T = int(input())
for i in range(T):
	print(solve())


def main():
	n = int(input())
	results = []
	for _ in range(n):
		(a1, a2, a3, a4) = list(map(int, input().split()))
		if a1 == 0:
			results.append(1)
			continue
		result = a1 + 2 * min(a2, a3)
		c1 = max(a2, a3) - min(a2, a3)
		if a1 + 1 <= c1:
			result += a1 + 1
		else:
			result += c1
			result += min(a1 - c1 + 1, a4)
		results.append(result)
	for r in results:
		print(r)
main()


kk = []
q11 = int(input())
for jjjj in range(q11):
	g = 0
	(u, m, n, b) = map(int, input().split())
	if u == 0:
		kk.append(1)
		continue
	g += u
	m1 = max(m, n)
	n1 = min(m, n)
	ff = u
	dd = u
	g += n1
	ff += n1
	dd -= n1
	if ff > m1:
		ff -= m1
		g += m1
		dd += m1
		if min([dd, ff]) >= b:
			g += b
		else:
			g += min([dd, ff]) + 1
			b = 0
	else:
		m1 -= ff
		g += ff
		dd += ff
		if m1 > 0 or b > 0:
			g += 1
	kk.append(g)
print(*kk, sep='\n')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	result = a
	bob = a
	ali = a
	a = 0
	if bob or ali:
		m = min(b, c)
		result += m * 2
		b -= m
		c -= m
	if b:
		result += min(b, bob + 1)
		bob = max(bob - b, -1)
	if c and bob >= 0:
		result += min(c, ali + 1)
		ali = max(ali - c, -1)
	if d and b >= 0 and (ali >= 0):
		result += min(d, bob + 1, ali + 1)
	print(result)


k = int(input())
res = []
while k > 0:
	a = input().split(' ')
	(a1, a2, a3, a4) = (int(a[0]), int(a[1]), int(a[2]), int(a[3]))
	cur = a1
	cur += min(a2, a3) * 2
	remain = max(a2, a3) - min(a2, a3)
	cur += min(a1 + 1, remain + a4)
	if a1 == 0:
		if a2 > 0 or a3 > 0 or a4 > 0:
			res.append(1)
		else:
			res.append(0)
	else:
		res.append(cur)
	k -= 1
for x in res:
	print(x)


def test_joke_array(array):
	o = array[0]
	a = array[1]
	b = array[2]
	x = array[3]
	if o == 0:
		return 1
	ret = o + 2 * min(a, b)
	ret += min(o + 1, abs(a - b) + x)
	return ret

def abs_diff(a, b):
	ret = a - b
	if ret < 0:
		ret = -ret
	return ret

def min(a, b):
	if a <= b:
		return a
	else:
		return b
cases = int(input())
out = []
for i in range(cases):
	array_str = input().split(' ')
	array = [int(x) for x in array_str]
	out.append(test_joke_array(array))
for result in out:
	print(result)


def max(a, b):
	if a > b:
		return a
	return b

def min(a, b):
	if a > b:
		return b
	return a
n = int(input())
result = []
for i in range(n):
	u = input().split()
	l = []
	for x in u:
		l.append(int(x))
	m = 0
	m += l[0] + 2 * min(l[1], l[2])
	s = sum(l)
	if l[0] < s - m:
		m += l[0] + 1
	else:
		m = s
	if l[0] == 0:
		m = 1
	result.append(m)
for x in result:
	print(x)


li = []
for e in range(int(input())):
	(a1, a2, a3, a4) = map(int, input().split())
	if a1 == 0:
		li.append(1)
	else:
		ans = a1 + min(a2, a3) * 2 + min(a1, abs(a2 - a3))
		if a1 > abs(a2 - a3):
			ans += min(a1 - abs(a2 - a3), a4)
		li.append(min(ans + 1, a1 + a2 + a3 + a4))
print(*li, sep='\n')


out = []
for _ in range(int(input())):
	(a1, a2, a3, a4) = map(int, input().split())
	if a1 == 0 and max(a2, a3, a4) > 0:
		out.append(1)
		continue
	ans = a1
	ba = a1
	bb = a1
	d = min(a2, a3)
	ans += d * 2
	a2 -= d
	a3 -= d
	if a2 > 0:
		d = min(a2, bb)
		ans += d
		bb -= d
		ba += d
		a2 -= d
	elif a3 > 0:
		d = min(a3, ba)
		ans += d
		ba -= d
		bb += d
		a3 -= d
	if max(a2, a3, a4) > 0:
		ans += min(ba + 1, bb + 1, a2 + a3 + a4)
	out.append(ans)
for i in out:
	print(i)


import math
n = input()
Output = []
for i in range(int(n)):
	a = input().split()
	for i in range(len(a)):
		a[i] = int(a[i])
	if a[0] == 0:
		Output.append(1)
	elif a[0] + a[1] - a[2] - a[3] >= -1 and a[0] + a[2] - a[1] - a[3] >= -1:
		Output.append(a[0] + a[1] + a[2] + a[3])
	else:
		Output.append(min(2 * min(a[1], a[2]) + 2 * a[0] + 1, a[0] + a[1] + a[2] + a[3]))
for i in Output:
	print(i)


n1 = int(input())
ans = []
for i in range(1, n1 + 1):
	ll = list(map(int, input().strip().split()))[:4]
	c = 0
	if ll[0] >= 1 and ll[1] == 0 and (ll[2] == 0) and (ll[3] == 0):
		c = ll[0]
	elif ll[0] == 0:
		c = 1
	elif ll[0] >= 1 and ll[3] >= 1 and (ll[2] == 0) and (ll[1] == 0):
		if ll[0] < ll[3]:
			c = 2 * ll[0] + 1
		elif ll[0] == ll[3]:
			c = 2 * ll[0]
		else:
			c = ll[3] + ll[0]
	else:
		min_val = min(ll[2], ll[1])
		if ll[0] < ll[3]:
			c = 2 * ll[0] + 1
			c += min_val * 2
		else:
			diff = abs(ll[1] - ll[2])
			c = min_val * 2 + ll[0]
			if diff + ll[3] > ll[0]:
				c += ll[0] + 1
			elif diff + ll[3] == ll[0]:
				c += ll[0]
			else:
				c += diff + ll[3]
	ans.append(c)
for k in ans:
	print(k, end='\n')


output_list = []
inp_tcs = int(input())
for i in range(inp_tcs):
	quan1 = 0
	quan2 = 0
	inp_tc = input()
	inp_tc = [int(k) for k in inp_tc.split()]
	(a1, a2, a3, a4) = (inp_tc[0], inp_tc[1], inp_tc[2], inp_tc[3])
	if a1 != 0:
		if a1 > max([a2, a3]) - min([a2, a3]):
			quan1 = max(a2, a3) - min(a2, a3)
			var1 = True
		elif a1 == max([a2, a3]) - min([a2, a3]) and a4 != 0:
			quan1 = a1 + 1
			var1 = False
		elif a1 < max([a2, a3]) - min([a2, a3]):
			quan1 = a1 + 1
			var1 = False
		else:
			quan1 = a1
			var1 = False
		if var1 == True:
			rem_mood = a1 - quan1
			if rem_mood >= a4:
				quan2 = a4
			else:
				quan2 = rem_mood + 1
		final_answer = a1 + min(a2, a3) * 2 + quan1 + quan2
		output_list.append(final_answer)
	else:
		output_list.append(1)
for i in output_list:
	print(i)


n = int(input())
l = []
for i in range(n):
	c = 0
	jk = 0
	pp = 0
	a = input().split(' ')
	jk += int(a[0])
	pp += int(a[0])
	if int(a[0]) == 0:
		if int(a[1]) > 0 or int(a[2]) > 0 or int(a[3]) > 0:
			c = 1
	if int(a[1]) < int(a[2]):
		jk += 2 * int(a[1])
		a[2] = int(a[2]) - int(a[1])
		a[1] = 0
		if int(a[2]) > 0 and pp >= 0:
			if int(a[2]) > pp + 1:
				jk += pp + 1
				pp = -1
			else:
				jk += int(a[2])
				pp -= int(a[2])
	else:
		jk += 2 * int(a[2])
		a[1] = int(a[1]) - int(a[2])
		a[2] = 0
		if int(a[1]) > 0 and pp >= 0:
			if int(a[1]) > pp + 1:
				jk += pp + 1
				pp = -1
			else:
				jk += int(a[1])
				pp -= int(a[1])
	if int(a[3]) > 0 and pp >= 0:
		if int(a[3]) > pp + 1:
			jk += pp + 1
		else:
			jk += int(a[3])
	if c == 1:
		jk = 1
	l.append(jk)
for i in range(len(l)):
	print(l[i])


t = int(input())
A = []
for i in range(t):
	a = [int(p) for p in input().split()]
	A.append(a)
for i in range(t):
	(a, b, c, d) = A[i]
	sum = a
	lanjut = True
	cek = False
	if a > 0:
		sum += min(b, c) * 2
		lmao = max(b, c) - min(b, c)
		if lmao - a < 0:
			sum += lmao
			cek = True
			residu = abs(lmao - a)
		else:
			sum += a
	else:
		sum += 1
		lanjut = False
	if lanjut and cek:
		sum += min(residu + 1, d)
	elif lanjut:
		d += max(b, c) - min(b, c) - a
		if d > 0:
			sum += 1
	print(sum)


for _ in range(int(input())):
	(a1, a2, a3, a4) = map(int, input().split())
	if a1 == 0:
		print(1)
	else:
		print(a1 + min(a2, a3) * 2 + min(a1 + 1, abs(a2 - a3) + a4))


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a == 0:
		print(1)
	else:
		print(a + min(b, c) * 2 + min(a + 1, abs(b - c) + d))


for __ in range(int(input())):
	(a1, a2, a3, a4) = map(int, input().split())
	x = min(a2, a3)
	y = max(a2, a3)
	if a1 == 0:
		print(1)
	elif x + a1 >= y:
		if x + a1 - y >= a4:
			print(a1 + a2 + a3 + a4)
		else:
			print(2 * (x + a1) + 1)
	else:
		print(2 * (x + a1) + 1)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	diff = abs(b - c)
	tjokes = a
	if a == 0:
		print(1)
	elif a - diff >= 0:
		if a - diff < d:
			tjokes += b + c + (a - diff) + 1
		else:
			tjokes += b + c + d
		print(tjokes)
	else:
		diff2 = a - diff
		tjokes += b + c + diff2 + 1
		print(tjokes)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split(' '))
	ans = a
	if a == 0:
		print(1)
	else:
		ans += 2 * min(b, c)
		k = min(b, c)
		b -= k
		c -= k
		ans += min(a + 1, b + c + d)
		print(ans)


for _ in range(int(input())):
	(t1, t2, t3, t4) = map(int, input().split())
	(a, b) = (t1, t1)
	res = t1
	x = min(t2, t3)
	if a >= 1:
		res += x * 2
		t2 -= x
		t3 -= x
	y = t2 + t3 + t4
	x = min(a, b, y)
	y -= x
	res += x
	if y > 0:
		res += 1
	print(res)


t = int(input())
for _ in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	if a == 0:
		print(1)
	elif b == c:
		if d == 0:
			print(a + 2 * b)
		else:
			print(a + 2 * b + min(d, a + 1))
	else:
		print(a + 2 * min(b, c) + min(a + 1, max(b, c) - min(b, c) + d))


tc = int(input())
for t in range(tc):
	(a, b, c, d) = [int(x) for x in input().split()]
	if a == 0:
		print(1)
	else:
		jocks = 0
		(b, c) = (min(b, c), max(b, c))
		c -= b
		jocks = a + 2 * b + min(a + 1, c + d)
		print(jocks)


for z in range(int(input())):
	ans = 0
	(a1, a2, a3, a4) = map(int, input().split())
	if a2 == a3 == 0:
		if a4 < a1 + 1:
			print(a1 + a4)
		else:
			print(a1 + a1 + 1)
	elif a1 == 0:
		print(1)
	else:
		ans += a1
		mi = min(a2, a3)
		ans += 2 * mi
		ma = a2 + a3 - mi - mi
		if ma + a4 >= a1 + 1:
			ans += a1 + 1
		else:
			ans += ma + a4
		print(ans)


t = int(input())
for _ in range(t):
	(a1, a2, a3, a4) = map(int, input().split())
	(a, b) = (0, 0)
	if a1 == 0:
		print(1)
		continue
	diff23 = abs(a2 - a3)
	jokes = a1
	if a2 + a3 + a4 == 0:
		print(jokes)
		continue
	jokes += min(a2, a3) * 2
	if a1 < diff23 + a4:
		jokes += a1 + 1
	elif a1 == diff23 + a4:
		jokes += a1
	else:
		jokes += diff23 + a4
	print(jokes)


for _ in range(int(input())):
	(X, Y, Z, d) = map(int, input().split())
	if X == 0:
		print(1 if Y or Z or d else 0)
		continue
	a = X
	t = min(Y, Z)
	Y -= t
	Z -= t
	a += 2 * t
	t = max(Y, Z)
	if t <= X:
		a += t + min(d, X - t + 1)
	else:
		a += X + 1
	print(a)


n = int(input())
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	if a == 0:
		print(1)
		continue
	sum = 0
	sum = sum + a + 2 * min(b, c)
	d = d + max(b, c) - min(b, c)
	print(sum + min(d, a + 1))


import sys

def solution():
	(a, b, c, d) = map(int, input().split())
	if a == 0:
		return 1
	m = min(b, c)
	res = a + 2 * m
	b -= m
	c -= m
	bad = b + c + d
	res += min(bad, a + 1)
	return res
t = int(input())
for _ in range(t):
	res = solution()
	print(res)


import sys
import math
import itertools

def solution():
	(a1, a2, a3, a4) = map(int, input().split())
	A = a1 + a2 + a3 + a4
	alisa = a1 + a2
	Bob = a1 + a3
	if a1 == 0:
		print(min(A, 1))
	else:
		answer = min(A, Bob * 2 + 1, alisa * 2 + 1)
		print(answer)
t = int(input())
for i in range(t):
	solution()


import math
t = int(input())

def pow(a, b):
	if b == 0:
		return 1
	elif b == 1:
		return a
	else:
		temp = pow(a, b // 2)
		temp = temp * temp
		if b % 2 == 1:
			temp = temp * a
		return temp
for _ in range(0, t):
	inputs = [int(num) for num in input().split()]
	a1 = inputs[0]
	a2 = inputs[1]
	a3 = inputs[2]
	a4 = inputs[3]
	jok = a1
	ali = a1
	bob = a1
	diff = abs(a2 - a3)
	if a1 == 0:
		print(1)
	else:
		ans = a1 + min(a2, a3) * 2 + min(a1, diff + a4 - 1) + 1
		print(ans)


x = int(input())
for i in range(x):
	(a, b, c, d) = map(int, input().split())
	bob = a
	out = a
	if a == 0:
		if b != 0 or c != 0 or d != 0:
			out = 1
	else:
		if b < c:
			(b, c) = (c, b)
		out = out + c * 2
		b = b - c
		c = 0
		if b > bob:
			out = out + bob + 1
			bob = -1
		else:
			out = out + b
			bob = bob - b
			b = 0
		if bob > -1 and d != 0:
			if bob >= d:
				out = out + d
			else:
				out = out + bob + 1
	print(out)


t = int(input())
for testcases in range(t):
	l = [int(x) for x in input().split()]
	if l[0] >= 1:
		diff = abs(l[1] - l[2])
		tot = sum(l)
		if diff >= l[0]:
			print(min(2 * l[0] + 2 * min(l[1], l[2]) + 1, tot))
		else:
			print(l[0] + l[1] + l[2] + min(l[0] - diff + 1, l[3]))
	else:
		print('1')


t = int(input())
for i in range(t):
	(a1, a2, a3, a4) = map(int, input().split())
	if a1 == 0:
		print(1)
	else:
		maxJokes = a1 + min(a2, a3) * 2
		restJokes = abs(a3 - a2)
		restJokes = restJokes + a4
		maxJokes += restJokes if a1 >= restJokes else a1 + 1
		print(maxJokes)


t = int(input())
for test in range(t):
	(a, b, c, d) = map(int, input().split())
	if a == 0:
		print(1)
	else:
		x = min(b, c)
		otv = a + 2 * x
		y = max(b, c) - x
		if a >= y:
			otv += min(a + 1, y + d)
		else:
			otv += min(a + 1, y)
		print(otv)


t = int(input())
for _ in range(t):
	(a1, a2, a3, a4) = map(int, input().split())
	if a1 == 0:
		ans = min(a2 + a3 + a4, 1)
	elif a1 >= max(a2, a3) - min(a2, a3) + a4:
		ans = a1 + a2 + a3 + a4
	else:
		ans = 2 * a1 + 2 * min(a2, a3) + 1
	print(ans)


t = int(input())
for i in range(t):
	(a1, a2, a3, a4) = map(int, input().split())
	a = a1 + a2 + a3 + a4
	b = min(a2, a3) * 2 + 2 * a1 + 1
	if a1 == 0:
		print(1)
	elif a > b:
		print(b)
	else:
		print(a)


for _ in range(int(input())):
	(a1, a2, a3, a4) = map(int, input().split())
	if a1 == 0 and a2 == 0 and (a3 == 0) and (a4 == 0):
		print(0)
	elif a1 == 0:
		print(1)
	elif a1 > 0 and a2 == 0 and (a3 == 0) and (a4 == 0):
		print(a1)
	elif a1 > 0 and a4 > 0 and (a2 == 0) and (a3 == 0):
		if a1 + a4 > 2 * a1 + 1:
			print(2 * a1 + 1)
		else:
			print(a1 + a4)
	elif a1 > 0 and a4 > 0 and (a2 == 0 or a3 == 0):
		if a1 + a2 + a3 + a4 > 2 * a1 + 1:
			print(2 * a1 + 1)
		else:
			print(a1 + a2 + a3 + a4)
	elif a2 < a3:
		if a1 + a2 + a3 + a4 > 2 * a1 + 2 * a2 + 1:
			print(2 * a1 + 2 * a2 + 1)
		else:
			print(a1 + a2 + a3 + a4)
	elif a1 + a2 + a3 + a4 > 2 * a1 + 2 * a3 + 1:
		print(2 * a1 + 2 * a3 + 1)
	else:
		print(a1 + a2 + a3 + a4)


def solve(a, b, c, d):
	if a == 0 and any([b, c, d]):
		print(1)
		return
	(b, c) = sorted([b, c])
	cnt = a + 2 * b + min(a + 1, c - b + d)
	print(cnt)
t = int(input())
for _ in range(t):
	h = list(map(int, input().split(' ')))
	solve(*h)


t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if a == 0:
		print(1)
		continue
	x = a + 2 * min(b, c)
	if c > b:
		c -= b
		b = 0
	else:
		b -= c
		c = 0
	x += min(a + 1, b + c + d)
	print(x)


t = int(input())
for i in range(t):
	(a1, a2, a3, a4) = map(int, input().split())
	if a1 == 0:
		print(1)
	else:
		b = min(a2, a3)
		c = abs(a2 - a3) + a4
		if a1 >= c:
			print(a1 + b * 2 + c)
		else:
			print(a1 + b * 2 + a1 + 1)


t = int(input())
for t in range(t):
	j = list(map(int, input().split()))[:4]
	ans = j[0]
	if j[0] > 0:
		ans += min(j[1], j[2]) * 2
		ans += min(j[0] + 1, abs(j[1] - j[2]) + j[3])
		print(ans)
	else:
		print(1)


def solve():
	(a, b, c, d) = map(int, input().split())
	if a == 0:
		print(1)
		return
	(x, y) = (a, a)
	ans = a + 2 * min(b, c)
	rem = max(b, c) - min(b, c)
	can = min(rem, a)
	ok = 0
	x += can
	y -= can
	if rem > can:
		ok = 1
	ans += can
	can = min(y, d)
	if d > can:
		ok = 1
	ans += can
	print(ans + ok)
t = int(input())
for _ in range(t):
	solve()


t = int(input())
while t > 0:
	(t1, t2, t3, t4) = map(int, input().split())
	T = t1 + t2 + t3 + t4
	A_mood = 0
	B_mood = 0
	ans = t1
	if t1 == 0:
		if t2 > 0 or t3 > 0 or t4 > 0:
			print(1)
		else:
			print(0)
	else:
		if t2 > t3:
			(t2, t3) = (t3, t2)
		ans = ans + 2 * t2
		t3 = t3 - t2
		ans = ans + min(t1 + 1, t3 + t4)
		print(ans)
	t = t - 1


T = int(input())
for _ in range(T):
	nums = list(map(int, input().split()))
	if nums[0] == 0:
		print(1)
	else:
		ans = nums[0] + min(nums[1], nums[2]) * 2 + min(nums[0] + 1, abs(nums[1] - nums[2]) + nums[3])
		print(ans)


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	mood = min(b, c)
	mood1 = max(b, c)
	if a == 0:
		if b + c + d == 0:
			print(0)
		else:
			print(1)
	else:
		ans = 0
		if mood1 == 0:
			print(a + min(a + 1, d))
		elif mood == 0:
			print(a + min(a + 1, mood1 + d))
		else:
			print(a + mood + min(a + mood + 1, mood1 + d))


n = int(input())
for i in range(n):
	(a1, a2, a3, a4) = map(int, input().split())
	if a1 == 0:
		print(1)
	elif a2 > a3:
		if a2 - a3 + a4 - a1 > 0:
			print(2 * a3 + 2 * a1 + 1)
		elif a2 - a3 + a4 - a1 == 0:
			print(2 * a3 + 2 * a1)
		else:
			print(a3 + a1 + a2 + a4)
	elif a3 - a2 + a4 - a1 > 0:
		print(2 * a2 + 2 * a1 + 1)
	elif a3 - a2 + a4 - a1 == 0:
		print(2 * a2 + 2 * a1)
	else:
		print(a3 + a1 + a2 + a4)


n1 = int(input())
for i in range(n1):
	(a, b, c, x) = map(int, input().split())
	if a == 0:
		print(1)
		continue
	sum = 0
	sum = sum + a + 2 * min(b, c)
	x = x + max(b, c) - min(b, c)
	print(sum + min(x, a + 1))


for n in range(int(input())):
	a = list(map(int, input().split()))
	if a[0] != 0:
		ans = a[0] + min(a[1], a[2]) * 2 + min(a[0] + 1, abs(a[1] - a[2]) + a[3])
	else:
		ans = 1
	print(ans)


t = int(input())
for _ in range(t):
	s = list(map(int, input().split()))
	a = [0, 0]
	a[0] = s[0]
	a[1] = s[0]
	arif = a[0] + 1
	if arif == 1:
		print(int(bool(sum(s[1:]))))
	elif s[3] == 0:
		sc = min(s[1], s[2])
		ost = max(s[1], s[2]) - sc
		print(s[0] + sc * 2 + min(ost, arif))
	else:
		sc = min(s[1], s[2])
		ost = max(s[1], s[2]) - sc
		ost2 = min(ost, arif)
		arif -= ost2
		if arif:
			s3 = min(s[3], arif)
			print(s[0] + sc * 2 + ost2 + s3)
		else:
			print(s[0] + sc * 2 + ost2)


t = int(input())
while t:
	t -= 1
	jokes = [int(i) for i in input().split(' ')]
	sum_jokes = sum(jokes)
	told_jokes = jokes[0] + 2 * (min(jokes[1], jokes[2]) if jokes[0] > 0 else 0) + min(jokes[0], max(jokes[1], jokes[2]) - min(jokes[1], jokes[2]) + jokes[3])
	print(told_jokes if told_jokes == sum_jokes else told_jokes + 1)


for t in range(int(input())):
	(a1, a2, a3, a4) = map(int, input().split())
	res = a1
	(alice, bob) = (a1, a1)
	if alice or bob:
		if a2 > a3:
			res += 2 * a3
			a2 -= a3
			a3 = 0
		elif a2 < a3:
			res += 2 * a2
			a3 -= a2
			a2 = 0
		else:
			res += a2 + a3
			(a2, a3) = (0, 0)
	if a2 and bob:
		if bob > a2:
			res += a2
			bob -= a2
			alice += a2
			a2 = 0
		elif bob < a2:
			res += bob
			bob = 0
			alice += bob
			a2 -= bob
		else:
			res += a2
			bob = 0
			alice += a2
			a2 = 0
	if a3 and alice:
		if alice > a3:
			res += a3
			alice -= a3
			bob += a3
			a3 = 0
		elif alice < a3:
			res += alice
			alice = 0
			bob += alice
			a3 -= alice
		else:
			res += a3
			alice = 0
			bob += a3
			a3 = 0
	if bob and alice and a4:
		res += min(a4, bob, alice)
		diff = min(a4, bob, alice)
		alice -= diff
		bob -= diff
		a4 -= diff
	if bob == 0 and a2 or (alice == 0 and a3) or ((bob == 0 or alice == 0) and a4):
		res += 1
	print(res)


for _ in range(int(input())):
	a = list(map(int, input().split()))
	ans = a[0] + 2 * min(a[1], a[2])
	cnt = a[0]
	if cnt == 0:
		print(1)
		continue
	if cnt >= abs(a[1] - a[2]) + a[3]:
		ans += abs(a[1] - a[2]) + a[3]
	else:
		ans += cnt + 1
	print(ans)