Naturally, the magical girl is very good at performing magic. She recently met her master wizard Devu, who gifted her R potions of red liquid, 
B potions of blue liquid, and G potions of green liquid.

- 
The red liquid potions have liquid amounts given by r[1], ..., r[R] liters. 

- 
The green liquid potions have liquid amounts given by g[1], ..., g[G] liters. 

- 
The blue liquid potions have liquid amounts given by b[1], ..., b[B] liters. 

She want to play with the potions by applying magic tricks on them. In a single magic trick, she will choose a particular color. Then she will pick all the potions of the chosen color and decrease the amount of liquid in them to half (i.e. if initial amount 
of liquid is x, then the amount after decrement will be x / 2  where division is integer division, e.g. 3 / 2 = 1 and 4 / 2 = 2). 

Because she has to go out of station to meet her uncle Churu, a wannabe wizard, only M minutes are left for her. In a single minute, she can perform at most one magic trick. Hence, she can perform at most M magic tricks.

She would like to minimize the maximum amount of liquid among all of Red, Green and Blue colored potions. Formally Let v be the maximum value of amount of liquid in any potion. We want to minimize the value of v.
Please help her.

-----Input-----

First line of the input contains an integer T denoting the number of test cases.
Then for each test case, we have four lines.

The first line contains four space separated integers R, G, B, M. The next 3 lines will describe the amount of different color liquids (r, g, b), which are separated by space.

-----Output-----
For each test case, print a single integer denoting the answer of the problem.

-----Constraints-----
-  1 ≤ T ≤ 1000 
-  1 ≤ R, G, B, M ≤ 100 
-  1 ≤ r[i], g[i], b[i] ≤ 10^9 

-----Example-----
Input:
3
1 1 1 1
1
2
3
1 1 1 1
2
4
6
3 2 2 2
1 2 3
2 4
6 8
Output:
2
4
4

-----Explanation-----
Example case 1. Magical girl can pick the blue potion and make its liquid amount half. So the potions will now have amounts 1 2 1. Maximum of these values is 2. Hence answer is 2.

Difficulty: MEDIUM_HARD
Tags: Constructive algorithms

Mathematics

Ad-hoc

Starter code:

Solutions:
t = int(input())
for _ in range(t):
	(r, g, b, m) = list(map(int, input().split()))
	R = sorted(list(map(int, input().split())))
	G = sorted(list(map(int, input().split())))
	B = sorted(list(map(int, input().split())))
	for _ in range(m):
		n = max(R[-1], G[-1], B[-1])
		if n == 0:
			print(n)
			break
		if n == R[-1]:
			R[-1] //= 2
		elif n == G[-1]:
			G[-1] //= 2
		else:
			B[-1] //= 2
	else:
		print(max(R[-1], G[-1], B[-1]))


T = int(input())

def findv(spells, M, R, G, B):
	r_0 = 0
	r_i = R
	g_0 = r_i
	g_i = g_0 + G
	b_0 = g_i
	b_i = b_0 + B
	for i in range(M):
		maxSpell = max(spells)
		index = spells.index(maxSpell)
		(start, end) = (0, 0)
		if r_0 <= index < r_i:
			(start, end) = (r_0, r_i)
		elif g_0 <= index < g_i:
			(start, end) = (g_0, g_i)
		else:
			(start, end) = (b_0, b_i)
		for i in range(start, end):
			spells[i] = maxSpell // 2
	print(max(spells))
for testcase in range(T):
	(R, G, B, M) = map(int, input().split())
	spells = []
	for i in range(3):
		spells += list(map(int, input().split()))
	findv(spells, M, R, G, B)


cases = int(input())
for i in range(0, cases):
	(R, G, B, M) = input().split()
	potionsR = list(map(int, input().split()))
	potionsG = list(map(int, input().split()))
	potionsB = list(map(int, input().split()))
	for j in range(0, int(M)):
		nR = int(max(potionsR))
		nG = int(max(potionsG))
		nB = int(max(potionsB))
		maxN = int(max([nR, nG, nB]))
		if nR == maxN:
			potionsR = [int(int(x) / 2) for x in potionsR]
		elif nG == maxN:
			potionsG = [int(int(x) / 2) for x in potionsG]
		elif nB == maxN:
			potionsB = [int(int(x) / 2) for x in potionsB]
	print(max([int(max(potionsR)), int(max(potionsG)), int(max(potionsB))]))


for _ in range(int(input())):
	(r, g, b, m) = list(map(int, input().split()))
	l = []
	for i in range(3):
		l1 = list(map(int, input().split()))
		l.append(l1)
	for i in range(m + 1):
		a = 0
		b = 0
		for j in range(3):
			m = max(l[j])
			if m > a:
				a = m
				b = j
		for k in range(len(l[b])):
			l[b][k] = l[b][k] // 2
	print(a)


def call(l, lenn):
	s = 0
	for i in range(lenn):
		l[i] = l[i] // 2
		s += l[i]
	return s
for _ in range(int(input())):
	(red, green, blue, m) = map(int, input().split())
	arrRed = list(map(int, input().split()))
	arrGreen = list(map(int, input().split()))
	arrBlue = list(map(int, input().split()))
	maxRed = max(arrRed)
	maxGreen = max(arrGreen)
	maxBlue = max(arrBlue)
	for i in range(m):
		if maxBlue >= maxGreen and maxBlue >= maxRed:
			maxBlue = maxBlue // 2
		elif maxRed >= maxGreen and maxRed >= maxBlue:
			maxRed = maxRed // 2
		elif maxGreen >= maxRed and maxGreen >= maxBlue:
			maxGreen = maxGreen // 2
		else:
			pass
	print(max(maxBlue, maxGreen, maxRed))


t = int(input())
for x in range(0, t):
	(r, g, b, m) = map(int, input().split())
	a = []
	c = []
	for i in range(0, 3):
		v = list(map(int, input().split()))
		v.sort(reverse=True)
		a.append(v)
	a.sort()
	for i in range(0, m):
		l = len(a[2])
		j = 0
		while j < l:
			a[2][j] = int(a[2][j] / 2)
			j += 1
		a.sort()
	for x in a:
		c.append(max(x))
	print(max(c))


for i in range(int(input())):
	(r, g, b, m) = list(map(int, input().split()))
	temp = []
	for _ in range(3):
		k = max(list(map(int, input().split())))
		temp.append(k)
	(x, y, z) = temp
	while m != 0:
		maxi = max(x, y, z)
		if x == maxi:
			x = x // 2
		elif y == maxi:
			y = y // 2
		else:
			z = z // 2
		m -= 1
	print(max(x, y, z))


tc = int(input())
while tc:
	rgbm = input().split()
	R = int(rgbm[0])
	G = int(rgbm[1])
	B = int(rgbm[2])
	M = int(rgbm[3])
	r_list = list(map(int, input().split()))
	g_list = list(map(int, input().split()))
	b_list = list(map(int, input().split()))
	if R == 0:
		r_max = float('inf')
	else:
		r_max = max(r_list)
	if G == 0:
		g_max = float('inf')
	else:
		g_max = max(g_list)
	if B == 0:
		b_max = float('inf')
	else:
		b_max = max(b_list)
	for i in range(M):
		if r_max >= g_max and r_max >= b_max:
			r_max /= 2
		elif g_max >= r_max and g_max >= b_max:
			g_max /= 2
		else:
			b_max /= 2
	print(int(max(r_max, g_max, b_max)))
	tc -= 1


from sys import stdin, stdout
import math
from collections import Counter
import functools
import time

def tr():
	return range(int(line()))

def sm():
	return map(int, line().split())

def ln():
	return list(sm())

def nl():
	return int(line())

def ssm():
	return map(str, line().split())

def line():
	return stdin.readline().rstrip()

def b(x):
	return bin(x).replace('0b', '')

def o(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + '\n')

def osp(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + ' ')

def ol(x):
	stdout.write(' '.join(map(str, x)))

def wline():
	return stdin.read().split()

def main():
	for _ in tr():
		(r, g, b, m) = sm()
		portions = []
		portions.append(max(ln()))
		portions.append(max(ln()))
		portions.append(max(ln()))
		for i in range(m):
			portions.sort()
			portions[2] = portions[2] // 2
		print(max(portions))
main()


def check(lst1, lst2, lst3):
	lst1 = list(reversed(lst1))
	lst2 = list(reversed(lst2))
	lst3 = list(reversed(lst3))
	if lst1 >= lst2 and lst1 >= lst3:
		return 'R'
	elif lst2 >= lst1 and lst2 >= lst3:
		return 'G'
	else:
		return 'B'

def devide_by_2(lst):
	for i in range(len(lst)):
		lst[i] = lst[i] // 2

def solve():
	k = 0
	while k < M:
		r = check(dct['R'], dct['G'], dct['B'])
		devide_by_2(dct[r])
		k = k + 1
	ans = max(dct['R'][-1], dct['G'][-1], dct['B'][-1])
	print(ans)
t = int(input())
for _ in range(t):
	(R, G, B, M) = list(map(int, input().split()))
	dct = {}
	for ele in ['R', 'G', 'B']:
		dct[ele] = sorted(list(map(int, input().split())))
	solve()


T = int(input())
for _ in range(T):
	(r, g, b, m) = map(int, input().split())
	R = list(map(int, input().split()))
	G = list(map(int, input().split()))
	B = list(map(int, input().split()))
	while m > 0:
		m2 = max(G)
		m1 = max(R)
		m3 = max(B)
		m4 = max(m1, m2, m3)
		if m4 == m1:
			for i in range(len(R)):
				R[i] = R[i] // 2
		elif m4 == m2:
			for i in range(len(G)):
				G[i] = G[i] // 2
		elif m4 == m3:
			for i in range(len(B)):
				B[i] = B[i] // 2
		m -= 1
	print(max(max(R), max(G), max(B)))


for _ in range(int(input())):
	(r1, g1, b1, m) = map(int, input().split())
	r = [int(i) for i in input().split()]
	g = [int(j) for j in input().split()]
	b = [int(k) for k in input().split()]
	while m > 0:
		if max(max(r), max(g), max(b)) in r:
			r = [i // 2 for i in r]
		elif max(max(r), max(g), max(b)) in g:
			g = [j // 2 for j in g]
		else:
			b = [k // 2 for k in b]
		m -= 1
	print(max(max(r), max(g), max(b)))


import sys
import math
import heapq

def half(arr):
	for i in range(len(arr)):
		arr[i] //= 2
	return arr

def main(arr, m):
	(x, y, z) = arr
	x.sort()
	y.sort()
	z.sort()
	while m != 0:
		(a, b, c) = (x[-1], y[-1], z[-1])
		s = max(a, b, c)
		if s == a:
			half(x)
		elif s == b:
			half(y)
		else:
			half(z)
		m -= 1
	return max(x[-1], y[-1], z[-1])
for i in range(int(input())):
	(r, g, b, m) = list(map(int, input().split()))
	arr = []
	for j in range(3):
		c = list(map(int, input().split()))
		arr.append(c)
	print(main(arr, m))


def soln(r, g, b, m, rrr, grr, brr):
	rmax = max(rrr)
	gmax = max(grr)
	bmax = max(brr)
	for i in range(m):
		cur = max([rmax, gmax, bmax])
		if rmax == cur:
			rmax //= 2
		elif gmax == cur:
			gmax //= 2
		else:
			bmax //= 2
	return max([rmax, gmax, bmax])
for _ in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	rrr = list(map(int, input().split()))
	grr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	print(soln(r, g, b, m, rrr, grr, brr))


from math import floor
testCases = int(input())
for testCase in range(testCases):
	(R, G, B, M) = map(int, input().split())
	r = list(map(int, input().split()))
	g = list(map(int, input().split()))
	b = list(map(int, input().split()))
	rMax = max(r)
	gMax = max(g)
	bMax = max(b)
	for _ in range(M):
		if rMax == max(rMax, gMax, bMax):
			rMax = floor(rMax / 2)
		elif gMax == max(rMax, gMax, bMax):
			gMax = floor(gMax / 2)
		elif bMax == max(rMax, gMax, bMax):
			bMax = floor(bMax / 2)
	print(max(rMax, gMax, bMax))


t = int(input())
for i in range(t):
	(r, g, b, m) = map(int, input().split())
	rp = list(map(int, input().split()))
	gp = list(map(int, input().split()))
	bp = list(map(int, input().split()))
	for i in range(m):
		if max(rp) >= max(gp) and max(rp) >= max(bp):
			rp = [x // 2 for x in rp]
		elif max(gp) >= max(rp) and max(gp) >= max(bp):
			gp = [x // 2 for x in gp]
		else:
			bp = [x // 2 for x in bp]
	p = rp + gp + bp
	print(max(p))


import math

def half(n):
	return n // 2

def main(arr, m):
	(a, b, c) = arr
	while m != 0:
		s = max(a, b, c)
		if s == a:
			a = half(a)
		elif s == b:
			b = half(b)
		else:
			c = half(c)
		m -= 1
	return max(a, b, c)
for i in range(int(input())):
	(r, g, b, m) = list(map(int, input().split()))
	arr = []
	for j in range(3):
		c = max(list(map(int, input().split())))
		arr.append(c)
	print(main(arr, m))


t = int(input())
while t > 0:
	(r, g, q, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = list(map(int, input().split()))
	for i in range(m):
		x = max(a)
		y = max(b)
		z = max(c)
		if x >= y and x >= z:
			for j in range(r):
				a[j] //= 2
		elif y >= x and y >= z:
			for j in range(g):
				b[j] //= 2
		else:
			for j in range(q):
				c[j] //= 2
	x = max(a)
	y = max(b)
	z = max(c)
	print(max(x, max(y, z)))
	t -= 1


def potion(m, l):
	for _ in range(m):
		l.sort()
		l[2] = l[2] // 2
	return max(l)
try:
	tstc = int(input())
	for t in range(tstc):
		(r, g, b, m) = map(int, input().split())
		red = [int(i) for i in input().split()]
		green = [int(i) for i in input().split()]
		blue = [int(i) for i in input().split()]
		print(potion(m, [max(red), max(green), max(blue)]))
except:
	pass


for _ in range(int(input())):
	(r, b, g, m) = list(map(int, input().split()))
	red = sorted(map(int, input().split()), reverse=True)
	blue = sorted(map(int, input().split()), reverse=True)
	green = sorted(map(int, input().split()), reverse=True)
	(x, y, z) = (red[0], blue[0], green[0])
	for _ in range(m):
		k = max(x, y, z)
		if k == x:
			x = x // 2
		elif k == y:
			y = y // 2
		else:
			z = z // 2
	print(max(x, y, z))


t = int(input())
for i in range(t):
	(r, g, b, m) = map(int, input().split())
	a = max(list(map(int, input().split())))
	b = max(list(map(int, input().split())))
	c = max(list(map(int, input().split())))
	l = [a, b, c]
	while m:
		x = max(l)
		l.append(x // 2)
		l.remove(x)
		m -= 1
	print(max(l))


t = int(input())
for ijk in range(0, t):
	(r, g, b, m) = list(map(int, input().strip().split()))
	ra = list(map(int, input().strip().split()))
	rb = list(map(int, input().strip().split()))
	rc = list(map(int, input().strip().split()))
	a = [max(ra), max(rb), max(rc)]
	for i in range(0, m):
		j = a.index(max(a))
		a[j] = a[j] // 2
	print(max(a))


for _ in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	R = max(list(map(int, input().split())))
	G = max(list(map(int, input().split())))
	B = max(list(map(int, input().split())))
	cur = [R, G, B]
	while m > 0:
		m -= 1
		index = cur.index(max(cur))
		cur[index] //= 2
	print(max(cur))


t = int(input())
for i in range(t):
	line = input()
	pui = line.split()
	r = int(pui[0])
	g = int(pui[1])
	b = int(pui[2])
	m = int(pui[3])
	ar = list()
	for i in range(3):
		line = input()
		pui = line.split()
		pui = [int(j) for j in pui]
		ar.append(pui)
	for i in range(m):
		R = max(ar[0])
		G = max(ar[1])
		B = max(ar[2])
		if R >= G and R >= B:
			for j in range(len(ar[0])):
				ar[0][j] = int(ar[0][j] / 2)
		elif G >= R and G >= B:
			for j in range(len(ar[1])):
				ar[1][j] = int(ar[1][j] / 2)
		elif B >= G and B >= R:
			for j in range(len(ar[2])):
				ar[2][j] = int(ar[2][j] / 2)
	R = max(ar[0])
	G = max(ar[1])
	B = max(ar[2])
	ans = max(R, G, B)
	print(ans)


T = int(input())
for i in range(T):
	(R, G, B, M) = list(map(int, input().split()))
	Rarray = list(map(int, input().split()))
	Garray = list(map(int, input().split()))
	Barray = list(map(int, input().split()))
	while M > 0:
		flag = 0
		maxR = max(Rarray)
		maxG = max(Garray)
		maxB = max(Barray)
		maximum = []
		if maxR >= maxG and maxR >= maxB:
			maximum = Rarray.copy()
			flag = 1
		elif maxG >= maxR and maxG >= maxB:
			maximum = Garray.copy()
			flag = 2
		elif maxB >= maxG and maxB >= maxR:
			maximum = Barray.copy()
			flag = 3
		maximum = [num // 2 for num in maximum]
		if flag == 1:
			Rarray = maximum.copy()
		elif flag == 2:
			Garray = maximum.copy()
		elif flag == 3:
			Barray = maximum.copy()
		M -= 1
	print(max(max(Rarray), max(Garray), max(Barray)))


t = int(input())
for _ in range(t):
	(R, G, B, M) = [int(x) for x in input().split(' ')]
	r = [int(x) for x in input().split(' ')]
	g = [int(x) for x in input().split(' ')]
	b = [int(x) for x in input().split(' ')]
	r.sort(reverse=True)
	g.sort(reverse=True)
	b.sort(reverse=True)
	for y in range(M):
		m = max(r[0], g[0], b[0])
		if m == r[0]:
			r = [x // 2 for x in r]
		elif m == g[0]:
			g = [x // 2 for x in g]
		elif m == b[0]:
			b = [x // 2 for x in b]
	print(max(r[0], g[0], b[0]))


test = int(input())
for z in range(test):
	(r, g, b, m) = map(int, input().split())
	R = list(map(int, input().split()))
	G = list(map(int, input().split()))
	B = list(map(int, input().split()))
	rmax = max(R)
	gmax = max(G)
	bmax = max(B)
	A = [rmax, gmax, bmax]
	while m > 0:
		A.sort()
		A[-1] = A[-1] // 2
		m -= 1
	print(max(A))


t = int(input())
while t > 0:
	(r, g, b, m) = [int(x) for x in input().split()]
	R = [int(x) for x in input().split()]
	G = [int(x) for x in input().split()]
	B = [int(x) for x in input().split()]
	rr = max(R)
	gg = max(G)
	bb = max(B)
	arr = [rr, gg, bb]
	while m > 0:
		arr = sorted(arr)
		arr[2] = arr[2] // 2
		m = m - 1
	t = t - 1
	arr = sorted(arr)
	x = arr[2]
	print(x)


for _ in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	red = max(list(map(int, input().split())))
	blue = max(list(map(int, input().split())))
	green = max(list(map(int, input().split())))
	while m:
		if red >= blue and red >= green:
			red = red // 2
		elif blue >= red and blue >= green:
			blue = blue // 2
		else:
			green = green // 2
		m -= 1
	print(max(red, blue, green))


t = int(input())
while t:
	t -= 1
	(r, g, b, m) = map(int, input().split())
	R = max(list(map(int, input().split())))
	G = max(list(map(int, input().split())))
	B = max(list(map(int, input().split())))
	L = [R, G, B]
	for i in range(m):
		L.sort()
		L[-1] //= 2
	L.sort()
	print(L[-1])


T = int(input())

def max_list(a):
	m = 0
	for i in a:
		if i > m:
			m = i
	return m

def half(b):
	j = []
	for i in b:
		j.append(i // 2)
	return j
for i in range(T):
	(a, b, c, d) = input().split()
	r = int(a)
	g = int(b)
	b = int(c)
	m = int(d)
	r_list = list(map(int, input().split()))
	g_list = list(map(int, input().split()))
	b_list = list(map(int, input().split()))
	for j in range(m):
		r_max = max_list(r_list)
		g_max = max_list(g_list)
		b_max = max_list(b_list)
		if r_max >= g_max and r_max >= b_max:
			r_list = half(r_list)
		elif g_max >= r_max and g_max >= b_max:
			g_list = half(g_list)
		else:
			b_list = half(b_list)
	r_max = max_list(r_list)
	g_max = max_list(g_list)
	b_max = max_list(b_list)
	if r_max >= g_max and r_max >= b_max:
		print(r_max)
	elif g_max >= r_max and g_max >= b_max:
		print(g_max)
	else:
		print(b_max)


for _ in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	rp = list(map(int, input().split()))
	gp = list(map(int, input().split()))
	bp = list(map(int, input().split()))
	rp.sort(reverse=True)
	bp.sort(reverse=True)
	gp.sort(reverse=True)
	main = [rp, bp, gp]
	main.sort(reverse=True)
	for i in range(m):
		for j in range(len(main[0])):
			main[0][j] //= 2
		main.sort(reverse=True)
	print(main[0][0])


for _ in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	Rarr = list(map(int, input().split()))
	Garr = list(map(int, input().split()))
	Barr = list(map(int, input().split()))
	while m > 0:
		Rmax = max(Rarr)
		Gmax = max(Garr)
		Bmax = max(Barr)
		if Rmax == max(Gmax, Rmax, Bmax):
			Rarr = [int(i / 2) for i in Rarr]
		elif Gmax == max(Gmax, Rmax, Bmax):
			Garr = [int(i / 2) for i in Garr]
		else:
			Barr = [int(i / 2) for i in Barr]
		m -= 1
	total = Rarr + Garr + Barr
	total.sort()
	print(total[-1])


T = int(input())
for i in range(T):
	[r, g, b, k] = list(map(int, input().split()))
	s = list(map(int, input().split()))
	t = list(map(int, input().split()))
	c = list(map(int, input().split()))
	j = sorted([max(s), max(t), max(c)])
	for i in range(k):
		j[-1] = int(j[-1] / 2)
		j = sorted(j)
	print(j[-1])


t = int(input())
for tt in range(t):
	(r, g, b, m) = map(int, input().split())
	redlist = list(map(int, input().split()))
	greenlist = list(map(int, input().split()))
	bluelist = list(map(int, input().split()))
	while m > 0:
		if max(redlist) >= max(bluelist) and max(redlist) >= max(greenlist):
			for i in range(len(redlist)):
				redlist[i] //= 2
			m -= 1
		elif max(bluelist) >= max(redlist) and max(bluelist) >= max(greenlist):
			for i in range(len(bluelist)):
				bluelist[i] //= 2
			m -= 1
		elif max(greenlist) >= max(redlist) and max(greenlist) >= max(bluelist):
			for i in range(len(greenlist)):
				greenlist[i] //= 2
			m -= 1
	maxval = []
	maxval.append(max(redlist))
	maxval.append(max(bluelist))
	maxval.append(max(greenlist))
	print(max(maxval))


for j in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	x = []
	for i in range(3):
		a = list(map(int, input().split()))
		x.append(a)
	while m != 0:
		c = []
		for i in x:
			c.append(max(i))
		z = c.index(max(c))
		for i in range(len(x[z])):
			x[z][i] = x[z][i] // 2
		m -= 1
	am = []
	for i in x:
		am += i
	print(max(am))


for _ in range(int(input())):
	(R, G, B, M) = map(int, input().split())
	r = list(map(int, input().split()))
	g = list(map(int, input().split()))
	b = list(map(int, input().split()))
	arr = [max(r), max(g), max(b)]
	for i in range(M):
		arr.sort()
		arr[-1] //= 2
	arr.sort()
	print(arr[-1])


t = int(input())
for _ in range(t):
	(r, g, b, m) = list(map(int, input().split()))
	R = list(map(int, input().split()))
	G = list(map(int, input().split()))
	B = list(map(int, input().split()))
	m1 = max(R)
	m2 = max(G)
	m3 = max(B)
	while m > 0:
		ma = max(m1, m2, m3)
		if m1 == ma:
			m1 = m1 // 2
		elif m2 == ma:
			m2 = m2 // 2
		else:
			m3 = m3 // 2
		m -= 1
	print(max(m1, m2, m3))


T = int(input())
for _ in range(T):
	(R, G, B, M) = map(int, input().split())
	r = list(map(int, input().split()))
	g = list(map(int, input().split()))
	b = list(map(int, input().split()))
	potions = {'red': r, 'green': g, 'blue': b}
	for _ in range(M):
		color = max(potions.keys(), key=lambda c: max(potions[c]))
		magic = potions[color]
		for i in range(len(magic)):
			magic[i] = magic[i] // 2
	print(max(max(r), max(g), max(b)))


for _ in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	a = max(list(map(int, input().split())))
	b = max(list(map(int, input().split())))
	c = max(list(map(int, input().split())))
	l = [a, b, c]
	while m:
		temp = max(l)
		l.append(temp // 2)
		l.remove(temp)
		m -= 1
	print(max(l))


T = int(input())
for t in range(T):
	(R, G, B, M) = map(int, input().split())
	r = list(map(int, input().split()))
	g = list(map(int, input().split()))
	b = list(map(int, input().split()))
	r_max = max(r)
	g_max = max(g)
	b_max = max(b)
	v = max(r_max, g_max, b_max)
	while M > 0:
		if r_max == v:
			r_max //= 2
		elif g_max == v:
			g_max //= 2
		else:
			b_max //= 2
		M -= 1
		v = max(r_max, g_max, b_max)
	print(v)


for t in range(int(input())):
	(r, g, b, m) = map(int, input().split(' '))
	rr = max(list(map(int, input().split(' '))))
	gg = max(list(map(int, input().split(' '))))
	bb = max(list(map(int, input().split(' '))))
	lst = sorted([rr, gg, bb])
	for i in range(m):
		lst[-1] = lst[-1] // 2
		lst = sorted(lst)
	print(lst[-1])


for _ in range(int(input())):
	(R, G, B, M) = map(int, input().split())
	l = []
	r = list(map(int, input().split()))
	rm = max(r)
	g = list(map(int, input().split()))
	gm = max(g)
	b = list(map(int, input().split()))
	bm = max(b)
	l = [rm, gm, bm]
	l.sort()
	for i in range(M):
		l[-1] //= 2
		l.sort()
	print(l[-1])


def great(r, g, b):
	if r > g and r > b:
		return (int(r / 2), g, b)
	elif g > b:
		return (r, int(g / 2), b)
	else:
		return (r, g, int(b / 2))
t = int(input())
while t:
	t -= 1
	(r, g, b, m) = map(int, input().split())
	r1 = list(map(int, input().split()))
	g1 = list(map(int, input().split()))
	b1 = list(map(int, input().split()))
	r = max(r1)
	g = max(g1)
	b = max(b1)
	for i in range(m):
		(r, g, b) = great(r, g, b)
	print(max(r, g, b))


mod = 10 ^ 8 + 7
import numpy as np
for _ in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = list(map(int, input().split()))
	(l1, l2, l3) = (max(a), max(b), max(c))
	for _ in range(m):
		high = max([l1, l2, l3])
		if high == l1:
			l1 //= 2
		elif high == l2:
			l2 //= 2
		else:
			l3 //= 2
	print(max([l1, l2, l3]))


for _ in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	rli = list(map(int, input().split()))
	gli = list(map(int, input().split()))
	bli = list(map(int, input().split()))
	while m > 0:
		rmx = max(rli)
		gmx = max(gli)
		bmx = max(bli)
		mx = max(rmx, gmx, bmx)
		m -= 1
		if mx == rmx:
			rli = [x // 2 for x in rli]
		elif mx == gmx:
			gli = [x // 2 for x in gli]
		elif mx == bmx:
			bli = [x // 2 for x in bli]
	rmx = max(rli)
	gmx = max(gli)
	bmx = max(bli)
	print(max(rmx, gmx, bmx))


t = int(input())
for _ in range(t):
	(R, B, G, M) = list(map(int, input().split()))
	R = list(map(int, input().split()))
	B = list(map(int, input().split()))
	G = list(map(int, input().split()))
	(r, b, g) = (max(R), max(B), max(G))
	for _ in range(M):
		if r >= b and r >= g:
			r = r // 2
		elif b >= r and b >= g:
			b = b // 2
		elif g >= r and g >= b:
			g = g // 2
	print(max(r, b, g))


def minimise(r, b, g, m):
	max1 = max(r)
	max2 = max(b)
	max3 = max(g)
	arr = [max1, max2, max3]
	while m != 0:
		to_be_changed = max(arr)
		itr2 = 0
		while itr2 != 3:
			if arr[itr2] == to_be_changed:
				arr[itr2] = arr[itr2] // 2
				break
			else:
				itr2 += 1
		m -= 1
	return max(arr)
test_cases = int(input())
while test_cases != 0:
	d1 = list(map(int, input().split()))
	d2 = list(map(int, input().split()))
	d3 = list(map(int, input().split()))
	d4 = list(map(int, input().split()))
	print(minimise(d2, d3, d4, d1[3]))
	test_cases -= 1


case = int(input())
for _ in range(case):
	(r, g, b, m) = map(int, input().split(' '))
	rl = list(map(int, input().split(' ')))
	gl = list(map(int, input().split(' ')))
	bl = list(map(int, input().split(' ')))
	mr = max(rl)
	mg = max(gl)
	mb = max(bl)
	while m > 0:
		if mr >= mg and mr >= mb:
			mr = mr // 2
		elif mg >= mr and mg >= mb:
			mg = mg // 2
		elif mb >= mr and mb >= mg:
			mb = mb // 2
		m -= 1
	res = max(mr, mg, mb)
	print(res)


t = int(input())
for i in range(t):
	[r, g, b, k] = list(map(int, input().split()))
	rs = list(map(int, input().split()))
	bs = list(map(int, input().split()))
	gs = list(map(int, input().split()))
	ans = sorted([max(rs), max(bs), max(gs)])
	for i in range(k):
		ans[-1] = int(ans[-1] / 2)
		ans = sorted(ans)
	print(ans[-1])


t = int(input())
for _ in range(t):
	(r, g, b, m) = list(map(int, input().split()))
	ar = list(map(int, input().split()))
	ag = list(map(int, input().split()))
	ab = list(map(int, input().split()))
	mxr = max(ar)
	mxg = max(ag)
	mxb = max(ab)
	for i in range(m):
		if mxr >= mxg and mxr >= mxb:
			mxr //= 2
		elif mxg >= mxr and mxg >= mxb:
			mxg //= 2
		else:
			mxb //= 2
	print(max(mxr, mxg, mxb))


T = int(input())
for t in range(T):
	(R, G, B, M) = list(map(int, input().split()))
	r = list(map(int, input().split()))
	g = list(map(int, input().split()))
	b = list(map(int, input().split()))
	amount_of_liquid = [max(r), max(g), max(b)]
	for i in range(M):
		max_amount_of_liquid = max(amount_of_liquid)
		for i in range(len(amount_of_liquid)):
			if amount_of_liquid[i] == max_amount_of_liquid:
				amount_of_liquid[i] = amount_of_liquid[i] // 2
				break
	print(max(amount_of_liquid))


for _ in range(int(input())):
	(R, G, B, M) = map(int, input().split())
	R_list = list(map(int, input().split()))
	G_list = list(map(int, input().split()))
	B_list = list(map(int, input().split()))
	for j in range(M):
		rSum = max(R_list)
		gSum = max(G_list)
		bSum = max(B_list)
		p = max(rSum, bSum, gSum)
		if p == rSum:
			for i in range(R):
				R_list[i] = R_list[i] // 2
		elif p == bSum:
			for i in range(B):
				B_list[i] = B_list[i] // 2
		else:
			for i in range(G):
				G_list[i] = G_list[i] // 2
	rSum = max(R_list)
	gSum = max(G_list)
	bSum = max(B_list)
	print(max(rSum, bSum, gSum))


for _ in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	r1 = list(map(int, input().split()))
	g1 = list(map(int, input().split()))
	b1 = list(map(int, input().split()))
	for i in range(m):
		m_r1 = max(r1)
		m_g1 = max(g1)
		m_b1 = max(b1)
		m_ovr = max(m_r1, m_g1, m_b1)
		if m_ovr == m_r1:
			r1 = [x // 2 for x in r1]
		elif m_ovr == m_g1:
			g1 = [x // 2 for x in g1]
		else:
			b1 = [x // 2 for x in b1]
	print(max(max(r1), max(g1), max(b1)))


t = int(input())
for _ in range(t):
	(r, g, b, m) = map(int, input().split())
	rl = list(map(int, input().split()))
	gl = list(map(int, input().split()))
	bl = list(map(int, input().split()))
	maxpots = [max(rl), max(gl), max(bl)]
	for _ in range(m):
		mp = max(maxpots)
		for i in range(3):
			if maxpots[i] == mp:
				maxpots[i] = maxpots[i] // 2
				break
	print(max(maxpots))


import math
t = int(input())
for i in range(t):
	(r, g, b, m) = map(int, input().split())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	z = list(map(int, input().split()))
	mx = max(x)
	my = max(y)
	mz = max(z)
	for j in range(m):
		if mx == max(mx, my, mz):
			mx = math.floor(mx / 2)
		elif my == max(mx, my, mz):
			my = math.floor(my / 2)
		elif mz == max(mx, my, mz):
			mz = math.floor(mz / 2)
	print(max(mx, my, mz))


import sys
t = int(input())
while t:
	(R, G, B, M) = [int(x) for x in sys.stdin.readline().split()]
	r = [int(x) for x in sys.stdin.readline().split()]
	g = [int(x) for x in sys.stdin.readline().split()]
	b = [int(x) for x in sys.stdin.readline().split()]
	r.sort(reverse=True)
	g.sort(reverse=True)
	b.sort(reverse=True)
	while M:
		temp = [r[0], g[0], b[0]]
		c = 0
		m = temp[0]
		for i in range(1, 3):
			if m < temp[i]:
				m = temp[i]
				c = i
		if c == 0:
			r = [int(x / 2) for x in r]
		elif c == 1:
			g = [int(x / 2) for x in g]
		else:
			b = [int(x / 2) for x in b]
		M = M - 1
	tmp = [r[0], g[0], b[0]]
	print(max(tmp))
	t = t - 1


for _ in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	ar = list(map(int, input().split()))
	ag = list(map(int, input().split()))
	ab = list(map(int, input().split()))
	(k1, k2, k3) = (max(ar), max(ag), max(ab))
	t1 = 0
	while t1 < m:
		if k1 >= k2:
			if k1 >= k3:
				for i in range(r):
					ar[i] //= 2
				k1 //= 2
			else:
				for i in range(b):
					ab[i] //= 2
				k3 //= 2
		elif k2 >= k3:
			for i in range(g):
				ag[i] //= 2
			k2 //= 2
		else:
			for i in range(b):
				ab[i] //= 2
			k3 //= 2
		t1 += 1
	if k1 >= k2:
		if k1 >= k3:
			print(k1)
		else:
			print(k3)
	elif k2 >= k3:
		print(k2)
	else:
		print(k3)


for _ in range(int(input())):
	(rr, gg, bb, m) = map(int, input().split())
	r = list(map(int, input().split()))
	g = list(map(int, input().split()))
	b = list(map(int, input().split()))
	sr = max(r)
	sg = max(g)
	sb = max(b)
	for i in range(m):
		h = max(sr, sg, sb)
		if h == sr:
			sr = sr // 2
		elif h == sg:
			sg = sg // 2
		else:
			sb = sb // 2
	print(max([sr, sg, sb]))


for i in range(int(input())):
	(R, G, B, M) = map(int, input().split())
	RP = list(map(int, input().split()))
	GP = list(map(int, input().split()))
	BP = list(map(int, input().split()))
	for i in range(M):
		if max(RP) >= max(GP) and max(RP) >= max(BP):
			RP = [x // 2 for x in RP]
		elif max(GP) >= max(RP) and max(GP) >= max(BP):
			GP = [x // 2 for x in GP]
		else:
			BP = [x // 2 for x in BP]
	p = RP + GP + BP
	print(max(p))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	r = list(map(int, input().split()))
	r2 = list(map(int, input().split()))
	r1 = max(r2)
	g2 = list(map(int, input().split()))
	g1 = max(g2)
	b2 = list(map(int, input().split()))
	b1 = max(b2)
	while r[3] > 0:
		temp = max(r1, g1, b1)
		if temp == r1:
			r1 //= 2
		elif temp == g1:
			g1 //= 2
		else:
			b1 //= 2
		r[3] -= 1
	print(max(r1, g1, b1))


for _ in range(int(input())):
	(r, g, b, m) = list(map(int, input().split()))
	r = list(map(int, input().split()))
	g = list(map(int, input().split()))
	b = list(map(int, input().split()))
	x = max(r)
	y = max(g)
	z = max(b)
	while m > 0:
		if x >= y and x >= z:
			x //= 2
		elif y >= x and y >= z:
			y //= 2
		else:
			z //= 2
		m -= 1
	print(max(x, y, z))


for _ in range(int(input())):
	(r, g, b, m) = map(int, input().split())
	r_lst = list(map(int, input().split()))
	g_lst = list(map(int, input().split()))
	b_lst = list(map(int, input().split()))
	for counter in range(m):
		maxi = max(max(r_lst), max(g_lst), max(b_lst))
		if maxi in r_lst:
			for elem in range(len(r_lst)):
				r_lst[elem] = r_lst[elem] // 2
		elif maxi in g_lst:
			for elem in range(len(g_lst)):
				g_lst[elem] = g_lst[elem] // 2
		else:
			for elem in range(len(b_lst)):
				b_lst[elem] = b_lst[elem] // 2
	maxi = max(max(r_lst), max(g_lst), max(b_lst))
	print(maxi)