Chef is playing with an expression which consists of integer operands and the following binary
Bitwise operators -  AND, OR and XOR. He is trying to figure out that what could be the Maximum possible answer of the expression, given that he can perform the operation in any order i.e not necessarily follow the rule of Precedence of operators while evaluating the expression.
After some time of consistent work Chef starts feeling exhausted and wants you to automate this process for him. Can you help him out?
The expression has Bitwise operators in symbol format:
- &  stands for AND 
- |   stands for OR
- ^   stands for XOR
NOTE : It is guaranteed that the expression will always be valid, also each OPERATOR will always be preceded and succeeded by an OPERAND.

-----Input:-----
- The first line of input contains a single integer $T$ denoting the number of test cases.
- The only line of input for each test case is a $string$ which is the Chef's expression to evaluate.

-----Output:-----
For each test case print a single integer i.e the maximum possible value of Chef's expression.

-----Constraints-----
- $1 \leq T \leq 100$.
- The number of OPERATORS in the expression will be atleast 1 and atmost 10.
- Each OPERAND may range from 0 to $10^9$.

-----Subtasks-----
- 10 points : The number of OPERATORS in the expression will be atmost 5.
- 20 points : The number of OPERATORS in the expression will be atmost 8.
- 70 points : Original constraints.

-----Sample Input:-----
2
3^40|10^2

92^95|56&2&3

-----Sample Output:-----
43

95

-----EXPLANATION:-----CASE 2 :
- If we first compute (56 & 2), the expression becomes 92^95|0&3, since (56 & 2) yields $0$.
- Now on computing (95 | 0), the expression becomes 92^95&3.
- Further on computing (95 & 3), the expression becomes 92^3.
- Finally (92 ^ 3) yields 95, which is the maximum value of the expression.

Difficulty: VERY_HARD

Starter code:

Solutions:
def value(a, b, c):
	if c == '&':
		return a & b
	elif c == '^':
		return a ^ b
	elif c == '|':
		return a | b

def break_rules(n, operator):
	if len(n) == 1:
		return n
	elif len(n) == 2:
		return [value(n[0], n[1], operator[0])]
	else:
		cont_ans = []
		for i in range(1, len(n)):
			l1 = n[:i]
			l2 = n[i:]
			o1 = operator[:i - 1]
			o2 = operator[i:]
			l1_ans = break_rules(l1, o1)
			l2_ans = break_rules(l2, o2)
			for k in l1_ans:
				for j in l2_ans:
					cont_ans.append(value(k, j, operator[i - 1]))
	return cont_ans
t = int(input())
while t > 0:
	operator = []
	num = []
	exp = input()
	temp = ''
	for i in range(len(exp)):
		if ord(exp[i]) > 47 and ord(exp[i]) < 58:
			temp = temp + exp[i]
		else:
			num.append(int(temp))
			temp = ''
			operator.append(exp[i])
		if i == len(exp) - 1:
			num.append(int(temp))
	t -= 1
	print(max(break_rules(num, operator)))


def extract(s):
	arr = []
	news = ''
	for x in s:
		try:
			x = int(x)
			news = news + str(x)
		except:
			arr = [news] + arr
			news = ''
			arr = [x] + arr
	arr = [news] + arr
	return arr
for i in range(int(input())):
	s = input()
	arr = extract(s)
	n = len(arr) // 2 + 1
	dp = [[0] * n for j in range(n)]
	for i in range(n):
		dp[i][i] = [int(arr[2 * i])]
	n = len(arr)
	for i in range(3, n + 1, 2):
		for j in range(0, n - i + 1, 2):
			ans = []
			for k in range(j + 2, i + j + 1, 2):
				for x in dp[j // 2][(k - 1) // 2]:
					for y in dp[k // 2][(i + j) // 2]:
						if arr[k - 1] == '&':
							ans += [x & y]
						if arr[k - 1] == '|':
							ans += [x | y]
						if arr[k - 1] == '^':
							ans += [x ^ y]
			if i != n:
				dp[j // 2][k // 2] = ans[:]
			else:
				print(max(ans))


dp = []

def solve(s, o, l, r):
	if dp[l][r] != None:
		return dp[l][r]
	if l == r:
		dp[l][r] = set([int(n[l])])
	else:
		res = set()
		for i in range(l, r):
			a = solve(s, o, l, i)
			b = solve(s, o, i + 1, r)
			for j in a:
				for k in b:
					res.add(eval(str(j) + o[i] + str(k)))
		dp[l][r] = res
	return dp[l][r]
for _ in range(int(input())):
	s = input()
	n = []
	o = []
	temp = ''
	for i in s:
		if i.isdigit():
			temp += i
		else:
			n.append(temp)
			o.append(i)
			temp = ''
	n.append(temp)
	dp = [[None for i in range(len(n))] for j in range(len(n))]
	s = list(solve(n, o, 0, len(n) - 1))
	print(max(s))


from collections import defaultdict
import time

def solve(s):
	if len(s) == 1:
		return int(s[0])
	temp = []
	for i in range(1, len(s) // 2 + 1):
		ans1 = solve(s[:len(s) - 2 * i])
		ans2 = solve(s[len(s) - 2 * i + 1:])
		op = s[len(s) - 2 * i]
		if op == '^':
			temp += [ans1 ^ ans2]
		elif op == '|':
			temp += [ans1 | ans2]
		else:
			temp += [ans1 & ans2]
	print(s, temp)
	return max(temp)

def extract(s):
	arr = []
	news = ''
	for x in s:
		try:
			x = int(x)
			news += str(x)
		except:
			arr += [news]
			news = ''
			arr += [x]
	arr += [news]
	return arr
t = int(input())
for _ in range(t):
	s = input()
	t1 = time.time()
	arr = extract(s)
	n = len(arr) // 2 + 1
	dp = [[0] * n for j in range(n)]
	for i in range(n):
		dp[i][i] = [int(arr[2 * i])]
	n = len(arr)
	for i in range(3, n + 1, 2):
		for j in range(0, n - i + 1, 2):
			ans = []
			for k in range(j + 2, i + j + 1, 2):
				for x in dp[j // 2][(k - 1) // 2]:
					for y in dp[k // 2][(i + j) // 2]:
						if arr[k - 1] == '&':
							ans += [x & y]
						if arr[k - 1] == '|':
							ans += [x | y]
						if arr[k - 1] == '^':
							ans += [x ^ y]
			if i != n:
				dp[j // 2][k // 2] = ans[:]
			else:
				print(max(ans))


def operation(a, b, o):
	if o == '&':
		return a & b
	elif o == '^':
		return a ^ b
	elif o == '|':
		return a | b

def ans(num, ope):
	if len(num) == 1:
		return num
	elif len(num) == 2:
		return [operation(num[0], num[1], ope[0])]
	else:
		xd = []
		for i in range(1, len(num)):
			(a, b) = (num[:i], num[i:])
			(o, p) = (ope[:i], ope[i:])
			(x, y) = (ans(a, o), ans(b, p))
			for j in x:
				for k in y:
					xd += [operation(j, k, ope[i - 1])]
		return xd
for _ in range(int(input())):
	s = input()
	num = []
	ope = []
	temp = ''
	for i in range(len(s)):
		xd = s[i].isnumeric()
		if xd:
			temp += s[i]
		else:
			num += [temp]
			ope += [s[i]]
			temp = ''
	num += [temp]
	num = list(map(int, num))
	print(max(ans(num, ope)))