s = input()
linenum = 0
coins = dict()
x = ""

def numToBinary(x):
	result = ""
	while x > 0:
		result = str(x%2) + result
		x = x/2
	return result

def findMin(coins, x):
	result = 0
	while (x != ""):
		if x[0] == "0": 
			x = x[1:]
			continue
		if x[0] == "1": 
			count = 1
			temp = 0
			added = False
			while count <= len(x):
				temp = pow(2, len(x)-count)
				if temp in coins:
					if coins[temp] >= pow(2,count-1):
						coins[temp] -= pow(2,count-1)
						added = True
						break
				count = count + 1
			if added: result += pow(2,count-1)
			else: break
	if x == "": return result
	else: return -1

for line in s.splitlines():
	print(line)
	if linenum == 1:
		for c in line.split(" "):
			if c in coins:
				coins[c] += 1
			else: coins[c] = 1
	if linenum > 1: 
		x = numToBinary(int(line))
		print(findMin(coins.copy(), x))
	linenum = linenum + 1