#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const long long ooo = 9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N = 200005;
const double PI = acos(-1.0);
const double eps = 1e-9;
int o(int x) { return x % _p; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void file_put() {
  freopen("filename.in", "r", stdin);
  freopen("filename.out", "w", stdout);
}
string F[300], E[300], T[300];
char st[10];
int n, op = 1;
int G(char *s) {
  int ans = 0;
  for (int i = (0); i <= (7); ++i) ans += ((s[i] - '0') << i);
  return ans;
}
bool cmp(const string &a, const string &b) {
  return a.length() == b.length() ? a < b : a.length() < b.length();
}
bool U(string &a, const string &b) {
  if (a == "" || cmp(b, a)) return a = b, 1;
  return 0;
}
void init() {
  F[0b11110000] = "x", F[0b11001100] = "y", F[0b10101010] = "z";
  while (op) {
    op = 0;
    for (int i = (0); i <= (255); ++i)
      if (E[i] != "") op += U(F[i], "(" + E[i] + ")");
    for (int i = (0); i <= (255); ++i)
      if (F[i] != "") op += U(F[i ^ 255], "!" + F[i]);
    for (int i = (0); i <= (255); ++i)
      if (F[i] != "") op += U(T[i], F[i]);
    for (int i = (0); i <= (255); ++i)
      if (T[i] != "")
        for (int j = (0); j <= (255); ++j)
          if (F[j] != "") op += U(T[i & j], T[i] + "&" + F[j]);
    for (int i = (0); i <= (255); ++i)
      if (T[i] != "") op += U(E[i], T[i]);
    for (int i = (0); i <= (255); ++i)
      if (E[i] != "")
        for (int j = (0); j <= (255); ++j)
          if (T[j] != "") op += U(E[i | j], E[i] + "|" + T[j]);
  }
}
int main() {
  init();
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i)
    scanf("%s", st), printf("%s\n", E[G(st)].c_str());
  return 0;
}
