#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, U;
char s[20];
struct table {
  int f, tp;
  string s;
  table(void) {}
  table(int _f, int t, string a) : f(_f), tp(t), s(a) {}
  inline bool operator<(const table &b) const {
    if (s.size() == b.s.size()) return s < b.s;
    return s.size() < b.s.size();
  }
};
table ans[N][4];
int vis[N][4];
set<table> S;
set<table>::iterator it;
vector<table> don;
inline void Push(int f, int tp, string s) {
  table u = table(f, tp, s);
  if (!vis[f][tp]) {
    vis[f][tp] = 1;
    ans[f][tp] = u;
    S.insert(u);
  } else if (u < ans[f][tp]) {
    S.erase(ans[f][tp]);
    ans[f][tp] = u;
    S.insert(u);
  }
}
int main(void) {
  Push(240, 0, "x");
  Push(204, 0, "y");
  Push(170, 0, "z");
  U = (1 << 8) - 1;
  while (!S.empty()) {
    it = S.begin();
    table cur = *it;
    S.erase(it);
    Push(cur.f, 0, '(' + cur.s + ')');
    if (cur.tp == 0) Push(cur.f ^ U, 0, '!' + cur.s);
    don.push_back(cur);
    for (table x : don) {
      if (cur.tp < 2 && x.tp < 2)
        Push(cur.f & x.f, 1, min(cur.s, x.s) + '&' + max(cur.s, x.s));
      Push(cur.f | x.f, 2, min(cur.s, x.s) + '|' + max(cur.s, x.s));
    }
  }
  for (int i = 0; i <= U; i++)
    ans[i][0] = min(ans[i][0], min(ans[i][1], ans[i][2]));
  scanf("%d", &n);
  while (n--) {
    scanf("%s", s);
    int f = 0;
    for (int i = 7; ~i; i--) f = f << 1 | (s[i] - '0');
    cout << ans[f][0].s << endl;
  }
  return 0;
}
