#include <bits/stdc++.h>
using namespace std;
const long long maxn = 256;
const long long inf = 2e9;
struct node {
  long long num, p;
  string s;
  inline bool operator>(const node& n1) const {
    if (s.length() != n1.s.length()) return s.length() > n1.s.length();
    return s > n1.s;
  }
  node(long long num, long long p, string s) : num(num), p(p), s(s) {}
};
inline bool cmp(const string& s1, const string& s2) {
  if (s1.empty()) return 1;
  if (s2.empty()) return 0;
  if (s1.length() != s2.length()) return s1.length() < s2.length();
  return s1 < s2;
}
string ans[maxn][3];
priority_queue<node, vector<node>, greater<node> > pq;
bool vis[maxn][3];
string s;
long long pw2[8], n, num, p;
inline string link(string s, long long p1, long long p2) {
  if (p1 < p2) return '(' + s + ')';
  return s;
}
inline void update(long long num, long long p, string s) {
  if (ans[num][p].empty() || cmp(s, ans[num][p])) {
    ans[num][p] = s;
    pq.push(node(num, p, s));
  }
  return;
}
inline void bfs() {
  ans[15][2] = "x";
  pq.push(node(15, 2, "x"));
  ans[51][2] = "y";
  pq.push(node(51, 2, "y"));
  ans[85][2] = "z";
  pq.push(node(85, 2, "z"));
  while (!pq.empty()) {
    node cur = pq.top();
    pq.pop();
    num = cur.num;
    p = cur.p;
    s = cur.s;
    if (vis[num][p]) continue;
    vis[num][p] = 1;
    update((~num) & (maxn - 1), 2, '!' + link(s, p, 2));
    for (int i = 0; i < maxn; i++)
      for (int j = 0; j < 3; j++) {
        if (!vis[i][j]) continue;
        update(num & i, 1, link(s, p, 1) + '&' + link(ans[i][j], j, 1));
        update(num & i, 1, link(ans[i][j], j, 1) + '&' + link(s, p, 1));
        update(num | i, 0, s + '|' + ans[i][j]);
        update(num | i, 0, ans[i][j] + '|' + s);
      }
  }
  return;
}
int main() {
  pw2[7] = 1;
  for (int i = 6; i >= 0; i--) pw2[i] = pw2[i + 1] * 2;
  bfs();
  cin >> n;
  while (n--) {
    cin >> s;
    num = 0;
    for (int i = 0; i < 8; i++) num += (s[i] - '0') * pw2[i];
    cout << *min_element(ans[num], ans[num] + 3, cmp) << "\n";
  }
  return 0;
}
