#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
struct node {
  string str;
  bool operator<(const node& x) const {
    if (str.size() == x.str.size()) return str < x.str;
    return str.size() < x.str.size();
  }
};
node dist[305][2];
int N;
int main() {
  scanf("%d", &N);
  int x = (1 << 3) + (1 << 2) + (1 << 1) + (1 << 0);
  int y = (1 << 5) + (1 << 4) + (1 << 1) + (1 << 0);
  int z = (1 << 6) + (1 << 4) + (1 << 2) + (1 << 0);
  dist[x][0].str = "x", dist[x ^ ((1 << 8) - 1)][0].str = "!x";
  dist[y][0].str = "y", dist[y ^ ((1 << 8) - 1)][0].str = "!y";
  dist[z][0].str = "z", dist[z ^ ((1 << 8) - 1)][0].str = "!z";
  priority_queue<pair<node, int>, vector<pair<node, int> >,
                 greater<pair<node, int> > >
      pq;
  pq.push(make_pair(dist[x][0], x << 1));
  pq.push(make_pair(dist[x ^ ((1 << 8) - 1)][0], (x ^ ((1 << 8) - 1)) << 1));
  pq.push(make_pair(dist[y][0], y << 1));
  pq.push(make_pair(dist[y ^ ((1 << 8) - 1)][0], (y ^ ((1 << 8) - 1)) << 1));
  pq.push(make_pair(dist[z][0], z << 1));
  pq.push(make_pair(dist[z ^ ((1 << 8) - 1)][0], (z ^ ((1 << 8) - 1)) << 1));
  while (!pq.empty()) {
    int dx = pq.top().second >> 1;
    int dy = pq.top().second & 1;
    node p = pq.top().first;
    pq.pop();
    if (dist[dx][dy] < p) continue;
    node cd, ncd;
    int st;
    cd.str = "!(" + p.str + ")";
    st = dx ^ ((1 << 8) - 1);
    if (dist[st][0].str.size() == 0 || cd < dist[st][0]) {
      dist[st][0] = cd;
      pq.push(make_pair(dist[st][0], st << 1));
    }
    for (int j = 0; j < (int)(256); ++j)
      for (int k = 0; k < (int)(2); ++k)
        if (dist[j][k].str.size()) {
          if (dy)
            cd.str = "(" + p.str + ")&";
          else
            cd.str = p.str + "&";
          if (k)
            cd.str += "(" + dist[j][k].str + ")";
          else
            cd.str += dist[j][k].str;
          if (k)
            ncd.str = "(" + dist[j][k].str + ")&";
          else
            ncd.str = dist[j][k].str + "&";
          if (dy)
            ncd.str += "(" + p.str + ")";
          else
            ncd.str += p.str;
          cd = min(cd, ncd);
          st = dx & j;
          if (dist[st][0].str.size() == 0 || cd < dist[st][0]) {
            dist[st][0] = cd;
            pq.push(make_pair(dist[st][0], st << 1));
          }
          cd.str = p.str + "|" + dist[j][k].str;
          ncd.str = dist[j][k].str + "|" + p.str;
          cd = min(cd, ncd);
          st = dx | j;
          if (dist[st][1].str.size() == 0 || cd < dist[st][1]) {
            dist[st][1] = cd;
            pq.push(make_pair(dist[st][1], st << 1 | 1));
          }
        }
  }
  for (int i = 0; i < (int)(N); ++i) {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    int cd = 0;
    for (int j = 0; j < (int)(s.size()); ++j)
      if (s[j] == '1') cd += 1 << j;
    if (dist[cd][0] < dist[cd][1])
      cout << dist[cd][0].str << endl;
    else
      cout << dist[cd][1].str << endl;
  }
  return 0;
}
