#include <bits/stdc++.h>
using namespace std;
string f[256], t[256], e[256];
string mn(string x, string y) {
  if (x == "") return y;
  if (y == "") return x;
  int lx = x.size(), ly = y.size();
  if (lx != ly) return lx < ly ? x : y;
  return x < y ? x : y;
}
char buf[10];
int n;
int main() {
  f[15] = "x";
  f[51] = "y";
  f[85] = "z";
  int s = 3, ok = 3;
  while (true) {
    for (int i = 0; i < 256; i++) {
      if (f[255 ^ i] == "") continue;
      f[i] = mn(f[i], "!" + f[255 ^ i]);
    }
    for (int i = 0; i < 256; i++) {
      if (e[i] == "") continue;
      f[i] = mn(f[i], "(" + e[i] + ")");
    }
    for (int i = 0; i < 256; i++) t[i] = mn(t[i], f[i]);
    for (int i = 0; i < 256; i++) {
      for (int j = 0; j < 256; j++) {
        if (t[i] == "" || t[j] == "") continue;
        t[i & j] = mn(t[i & j], t[i] + "&" + t[j]);
      }
    }
    for (int i = 0; i < 256; i++) e[i] = mn(e[i], t[i]);
    for (int i = 0; i < 256; i++) {
      for (int j = 0; j < 256; j++) {
        if (e[i] == "" || e[j] == "") continue;
        e[i | j] = mn(e[i | j], e[i] + "|" + e[j]);
      }
    }
    int ns = 0, nok = 0;
    for (int i = 0; i < 256; i++) {
      if (f[i] != "") {
        nok++;
        ns += f[i].size();
      }
      if (t[i] != "") {
        nok++;
        ns += t[i].size();
      }
      if (e[i] != "") {
        nok++;
        ns += e[i].size();
      }
    }
    if (s == ns && ok == nok) break;
    s = ns;
    ok = nok;
  }
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", buf);
    int res = 0;
    for (int j = 0; j < 8; j++) res |= (buf[7 - j] - '0') << j;
    cout << e[res] << endl;
  }
}
