#include<bits/stdc++.h>
using namespace std;
#define ll long long int 
#define pi pair<int ,int >
#define pl pair<ll ,ll >
#define pb push_back
#define vl vector <long long int >
#define vi vector <int >
#define vp vector <pi>
#define mod 1000000007
#define eps 1e-9
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define _ <<" "<<
#define forn(x,	n) for(int x = 0; x < n ;++ x) 
#define forn1n(x,n) for(int x = 1; x <= n ;++ x)
#define forn1(x,n) for(int x = 1; x < n ;++ x)
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#pragma GCC optimize ("Ofast")
void scan(){}
template<typename F, typename... R> void scan(F &f,R&... r){cin>>f;scan(r...);}
int di_; string dnms_, co_ = ",";
void debug_(){cout<<endl;}
template<typename F, typename... R> void debug_(F &f, R&... r){while(dnms_[di_] != ',')cout<<dnms_[di_++];di_++;cout<<": "<<f<<",";debug_(r...);}
#define debug(...) dnms_=#__VA_ARGS__+co_,di_=0,debug_(__VA_ARGS__)

template<class A, class B> ostream& operator<<(ostream& out, const pair<A, B> &a){
    return out<<"("<<a.first<<","<<a.second<<")";}
template<class A> ostream& operator<<(ostream& out, const vector<A> &a){
	out<<"";for(auto it=a.begin();it!=a.end();it++){if(it!=a.begin())out<<" ";out<<*it;}out<<"";
	return out;}
template<class A, class B> istream& operator>>(istream& in, pair<A,B> &a){in>>a.first>>a.second;return in;}
template<class A> istream& operator>>(istream& in, vector<A> &a){for(A &i:a)in>>i;return in;}

struct HASH{
  size_t operator()(const pair<int,int>&x)const{
    return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  }
};

string s;
vector<string>v;
const int N = 11;
unordered_map<pair<int,int>,vector<int> ,HASH>dp;
bool isop(char ch)
{
	return ch == '&'||ch == '|' || ch == '^'; // Check if it is an operator
}

int ansop(int x,int y,char op)
{
	// return the resultant after operation

	if(op=='|')
		return int(x)|int(y);
	else if(op=='&')
		return int(x)&int(y);
	else
		return int(x)^int(y);
}
vector<int> solve(int i, int j)
{
	if(i==j)
		return {stoi(v[i])}; // If we've only one element group then just return it
	if(!dp[{i,j}].empty())
		return dp[{i,j}];    // memoization
	vector<int> pa;          // possible answers
	for(int k=i+1;k<j;k+=2)
	{
		
		assert(isop(v[k][0]));            // assure that we're splitting on a operator
		vector<int> left = solve(i,k-1);  // possible reluts of left group
		vector<int> right = solve(k+1,j); // possible results of right grp

		for(auto& l:left)
			for(auto& r:right)
			{
				pa.pb(ansop(l,r,v[k][0])); // try every possible combination
			}
		sort(all(pa));
		pa.erase(unique(all(pa)),pa.end()); // remove repeating elements
	}
	return pa;
}
int main(){
	IOS

	int t;
	cin>>t;
	while(t--)
	{
		cin>>s;
		v.clear();
		int lindex=0;
		forn1(i,s.size())
		{
			if(isop(s[i]))
				{
					v.push_back(s.substr(lindex,i-lindex));
					v.push_back(s.substr(i,1));
					lindex=i+1;
				}
		}
		v.push_back(s.substr(lindex,s.size()-lindex));
		auto ans = solve(0,v.size()-1);
		cout<<ans.back()<<endl;
	}
	return 0;	
}