#include <bits/stdc++.h>
using namespace std;
int N;
string d[3][1005];
string ans[1005];
bool cmp(const string& a, const string& b) {
  if (a.length() != b.length())
    return a.length() < b.length();
  else
    return a < b;
}
struct Node {
  int id, x;
  string str;
  Node(int id = 0, int x = 0, string str = "") : id(id), x(x), str(str) {}
  bool operator<(const Node& n1) const { return cmp(n1.str, str); }
};
priority_queue<Node> q;
void update(int id1, int x1, string exp) {
  if (d[id1][x1] == "" || cmp(exp, d[id1][x1])) {
    d[id1][x1] = exp;
    q.push(Node(id1, x1, exp));
  }
}
bool vis[3][1005];
void init() {
  d[2][0b11110000] = "x";
  d[2][0b11001100] = "y";
  d[2][0b10101010] = "z";
  q.push(Node(2, 0b11110000, "x"));
  q.push(Node(2, 0b11001100, "y"));
  q.push(Node(2, 0b10101010, "z"));
  int id, x, x1;
  string exp;
  while (!q.empty()) {
    id = q.top().id;
    x = q.top().x;
    q.pop();
    if (vis[id][x]) continue;
    vis[id][x] = 1;
    exp = d[id][x];
    if (id == 2) {
      x1 = (~x) & 255;
      update(2, x1, "!" + exp);
      x1 = x;
      update(2, x1, "(" + exp + ")");
      for (int j = 0; j < 256; j++) {
        if (d[2][j] != "") {
          x1 = x & j;
          update(1, x1, exp + "&" + d[2][j]);
          update(1, x1, d[2][j] + "&" + exp);
          x1 = x | j;
          update(0, x1, exp + "|" + d[2][j]);
          update(0, x1, d[2][j] + "|" + exp);
        }
        if (d[1][j] != "") {
          x1 = x | j;
          update(0, x1, exp + "|" + d[1][j]);
          update(0, x1, d[1][j] + "|" + exp);
        }
      }
    }
    if (id == 1) {
      x1 = x;
      update(2, x1, "(" + exp + ")");
      for (int j = 0; j < 256; j++) {
        if (d[2][j] != "") {
          x1 = x & j;
          update(1, x1, exp + "&" + d[2][j]);
          x1 = x | j;
          update(0, x1, exp + "|" + d[2][j]);
          update(0, x1, d[2][j] + "|" + exp);
        }
        if (d[1][j] != "") {
          x1 = x | j;
          update(0, x1, exp + "|" + d[1][j]);
          update(0, x1, d[1][j] + "|" + exp);
        }
      }
    }
    if (id == 0) {
      x1 = x;
      update(2, x1, "(" + exp + ")");
      for (int j = 0; j < 256; j++) {
        if (d[2][j] != "") {
          x1 = x | j;
          update(0, x1, exp + "|" + d[2][j]);
        }
        if (d[1][j] != "") {
          x1 = x | j;
          update(0, x1, exp + "|" + d[1][j]);
        }
      }
    }
  }
  for (int j = 0; j < 256; j++) {
    ans[j] = d[0][j];
    if (cmp(d[1][j], ans[j])) ans[j] = d[1][j];
    if (cmp(d[2][j], ans[j])) ans[j] = d[2][j];
  }
}
int str2int(const string& s) {
  int res = 0;
  for (int i = 0; i < 8; i++) {
    if (s[i] == '1') res += (1 << i);
  }
  return res;
}
int main() {
  init();
  cin >> N;
  string t;
  while (N--) {
    cin >> t;
    int x = str2int(t);
    cout << ans[x] << endl;
  }
  return 0;
}
