#include <bits/stdc++.h>
using std::string;
string dp[1 << 8][3];
bool cmp(string a, string b) {
  if (a.length() == b.length()) return a < b;
  return a.length() < b.length();
}
struct expr {
  int mask, pr;
  string s;
  expr(int m, int p, string s) : mask(m), pr(p), s(s) {}
  bool operator<(const expr o) const {
    if (s == o.s) {
      if (mask != o.mask) return mask < o.mask;
      return pr < o.pr;
    }
    return cmp(s, o.s);
  }
  bool operator>(const expr o) const { return !(*this < o); }
};
std::queue<expr> q;
int pos[] = {1, 2, 4, 8, 16, 32, 64, 128};
void add(int mask, int pr, string s) {
  if (dp[mask][pr] == "" || cmp(s, dp[mask][pr])) {
    dp[mask][pr] = s;
    q.push(expr(mask, pr, s));
  }
}
int main() {
  int full = 0xff;
  add(pos[4] + pos[5] + pos[6] + pos[7], 2, "x");
  add(pos[2] + pos[3] + pos[6] + pos[7], 2, "y");
  add(pos[1] + pos[3] + pos[5] + pos[7], 2, "z");
  while (!q.empty()) {
    expr now = q.front();
    q.pop();
    if (now.pr == 2) {
      add((~now.mask) & full, 2, "!" + now.s);
      add(now.mask, 1, now.s);
    }
    if (now.pr == 1) {
      for (int mask = 0; mask < full; ++mask) {
        if (dp[mask][2] != "") {
          add(now.mask & mask, 1, dp[mask][2] + "&" + now.s);
          add(now.mask & mask, 1, now.s + "&" + dp[mask][2]);
        }
      }
      add(now.mask, 0, now.s);
      add(now.mask, 2, "(" + now.s + ")");
    }
    if (now.pr == 0) {
      for (int mask = 0; mask < full; ++mask) {
        for (int pr = 2; pr >= 1; --pr) {
          if (dp[mask][pr] != "") {
            add(now.mask | mask, 0, now.s + "|" + dp[mask][pr]);
            add(now.mask | mask, 0, dp[mask][pr] + "|" + now.s);
          }
        }
      }
      add(now.mask, 2, "(" + now.s + ")");
    }
  }
  int t;
  std::cin >> t;
  while (t--) {
    string s;
    std::cin >> s;
    int mask = 0;
    for (int i = 0; i < 8; ++i) {
      mask |= (s[i] - '0') << i;
    }
    std::cout << dp[mask][0] << std::endl;
  }
}
