#include <bits/stdc++.h>
using namespace std;
int n, a;
inline void read(int &x, int t) {
  static char c;
  for (c = getchar(); !('0' <= c && c <= '9'); c = getchar())
    ;
  for (x = 0; ('0' <= c && c <= '9'); c = getchar()) x = x * t + c - 48;
}
char ch[277][50] = {"!x&x",
                    "x&y&z",
                    "!z&x&y",
                    "x&y",
                    "!y&x&z",
                    "x&z",
                    "!y&x&z|!z&x&y",
                    "(y|z)&x",
                    "!y&!z&x",
                    "!y&!z&x|x&y&z",
                    "!z&x",
                    "!z&x|x&y",
                    "!y&x",
                    "!y&x|x&z",
                    "!(y&z)&x",
                    "x",
                    "!x&y&z",
                    "y&z",
                    "!x&y&z|!z&x&y",
                    "(x|z)&y",
                    "!x&y&z|!y&x&z",
                    "(x|y)&z",
                    "!x&y&z|!y&x&z|!z&x&y",
                    "(x|y)&z|x&y",
                    "!x&y&z|!y&!z&x",
                    "!y&!z&x|y&z",
                    "!x&y&z|!z&x",
                    "!z&x|y&z",
                    "!x&y&z|!y&x",
                    "!y&x|y&z",
                    "!(y&z)&x|!x&y&z",
                    "x|y&z",
                    "!x&!z&y",
                    "!x&!z&y|x&y&z",
                    "!z&y",
                    "!z&y|x&y",
                    "!x&!z&y|!y&x&z",
                    "!x&!z&y|x&z",
                    "!y&x&z|!z&y",
                    "!z&y|x&z",
                    "!(!x&!y|x&y|z)",
                    "!(!x&!y|x&y|z)|x&y&z",
                    "!z&(x|y)",
                    "!z&(x|y)|x&y",
                    "!x&!z&y|!y&x",
                    "!x&!z&y|!y&x|x&z",
                    "!y&x|!z&y",
                    "!z&y|x",
                    "!x&y",
                    "!x&y|y&z",
                    "!(x&z)&y",
                    "y",
                    "!x&y|!y&x&z",
                    "!x&y|x&z",
                    "!(x&z)&y|!y&x&z",
                    "x&z|y",
                    "!x&y|!y&!z&x",
                    "!x&y|!y&!z&x|y&z",
                    "!x&y|!z&x",
                    "!z&x|y",
                    "!x&y|!y&x",
                    "!x&y|!y&x|x&z",
                    "!(x&z)&y|!y&x",
                    "x|y",
                    "!x&!y&z",
                    "!x&!y&z|x&y&z",
                    "!x&!y&z|!z&x&y",
                    "!x&!y&z|x&y",
                    "!y&z",
                    "!y&z|x&z",
                    "!y&z|!z&x&y",
                    "!y&z|x&y",
                    "!(!x&!z|x&z|y)",
                    "!(!x&!z|x&z|y)|x&y&z",
                    "!x&!y&z|!z&x",
                    "!x&!y&z|!z&x|x&y",
                    "!y&(x|z)",
                    "!y&(x|z)|x&z",
                    "!y&z|!z&x",
                    "!y&z|x",
                    "!x&z",
                    "!x&z|y&z",
                    "!x&z|!z&x&y",
                    "!x&z|x&y",
                    "!(x&y)&z",
                    "z",
                    "!(x&y)&z|!z&x&y",
                    "x&y|z",
                    "!x&z|!y&!z&x",
                    "!x&z|!y&!z&x|y&z",
                    "!x&z|!z&x",
                    "!x&z|!z&x|x&y",
                    "!x&z|!y&x",
                    "!y&x|z",
                    "!(x&y)&z|!z&x",
                    "x|z",
                    "!(!y&!z|x|y&z)",
                    "!(!y&!z|x|y&z)|x&y&z",
                    "!x&!y&z|!z&y",
                    "!x&!y&z|!z&y|x&y",
                    "!x&!z&y|!y&z",
                    "!x&!z&y|!y&z|x&z",
                    "!y&z|!z&y",
                    "!y&z|!z&y|x&y",
                    "!(!x&!y|x&y|z)|!x&!y&z",
                    "!(!x&!y|x&y|z)|!x&!y&z|x&y&z",
                    "!x&!y&z|!z&(x|y)",
                    "!x&!y&z|!z&(x|y)|x&y",
                    "!x&!z&y|!y&(x|z)",
                    "!x&!z&y|!y&(x|z)|x&z",
                    "!y&(x|z)|!z&y",
                    "!y&z|!z&y|x",
                    "!x&(y|z)",
                    "!x&(y|z)|y&z",
                    "!x&z|!z&y",
                    "!x&z|y",
                    "!x&y|!y&z",
                    "!x&y|z",
                    "!(x&y)&z|!z&y",
                    "y|z",
                    "!x&(y|z)|!y&!z&x",
                    "!x&(y|z)|!y&!z&x|y&z",
                    "!x&(y|z)|!z&x",
                    "!x&z|!z&x|y",
                    "!x&(y|z)|!y&x",
                    "!x&y|!y&x|z",
                    "!x&y|!y&z|!z&x",
                    "x|y|z",
                    "!(x|y|z)",
                    "!(x|y|z)|x&y&z",
                    "!(!x&y|!y&x|z)",
                    "!(x|y|z)|x&y",
                    "!(!x&z|!z&x|y)",
                    "!(x|y|z)|x&z",
                    "!(!x&y|!y&x|z)|!y&x&z",
                    "!(x|y|z)|(y|z)&x",
                    "!y&!z",
                    "!y&!z|x&y&z",
                    "!(!x&y|z)",
                    "!y&!z|x&y",
                    "!(!x&z|y)",
                    "!y&!z|x&z",
                    "!(!x&y|z)|!y&x",
                    "!y&!z|x",
                    "!(!y&z|!z&y|x)",
                    "!(x|y|z)|y&z",
                    "!(!x&y|!y&x|z)|!x&y&z",
                    "!(x|y|z)|(x|z)&y",
                    "!(!x&z|!z&x|y)|!x&y&z",
                    "!(x|y|z)|(x|y)&z",
                    "!(!x&y|!y&x|z)|!x&y&z|!y&x&z",
                    "!(x|y|z)|(x|y)&z|x&y",
                    "!x&y&z|!y&!z",
                    "!y&!z|y&z",
                    "!(!x&y|z)|!x&y&z",
                    "!(!x&y|z)|y&z",
                    "!(!x&z|y)|!x&y&z",
                    "!(!x&z|y)|y&z",
                    "!(!x&y|z)|!x&y&z|!y&x",
                    "!y&!z|x|y&z",
                    "!x&!z",
                    "!x&!z|x&y&z",
                    "!(!y&x|z)",
                    "!x&!z|x&y",
                    "!x&!z|!y&x&z",
                    "!x&!z|x&z",
                    "!(!y&x|z)|!y&x&z",
                    "!(!y&x|z)|x&z",
                    "!(x&y|z)",
                    "!(x&y|z)|x&y&z",
                    "!z",
                    "!z|x&y",
                    "!x&!z|!y&x",
                    "!(x&y|z)|x&z",
                    "!y&x|!z",
                    "!z|x",
                    "!(!y&z|x)",
                    "!x&!z|y&z",
                    "!(!y&x|z)|!x&y",
                    "!x&!z|y",
                    "!(!y&z|x)|!y&x&z",
                    "!(!y&z|x)|x&z",
                    "!(!y&x|z)|!x&y|!y&x&z",
                    "!x&!z|x&z|y",
                    "!x&y|!y&!z",
                    "!(x&y|z)|y&z",
                    "!x&y|!z",
                    "!z|y",
                    "!(!x&!y&z|x&y)",
                    "!x&!z|!y&x|y&z",
                    "!x&y|!y&x|!z",
                    "!z|x|y",
                    "!x&!y",
                    "!x&!y|x&y&z",
                    "!x&!y|!z&x&y",
                    "!x&!y|x&y",
                    "!(!z&x|y)",
                    "!x&!y|x&z",
                    "!(!z&x|y)|!z&x&y",
                    "!(!z&x|y)|x&y",
                    "!(x&z|y)",
                    "!(x&z|y)|x&y&z",
                    "!x&!y|!z&x",
                    "!(x&z|y)|x&y",
                    "!y",
                    "!y|x&z",
                    "!y|!z&x",
                    "!y|x",
                    "!(!z&y|x)",
                    "!x&!y|y&z",
                    "!(!z&y|x)|!z&x&y",
                    "!(!z&y|x)|x&y",
                    "!(!z&x|y)|!x&z",
                    "!x&!y|z",
                    "!(!z&x|y)|!x&z|!z&x&y",
                    "!x&!y|x&y|z",
                    "!x&z|!y&!z",
                    "!(x&z|y)|y&z",
                    "!(!x&!z&y|x&z)",
                    "!x&!y|!z&x|y&z",
                    "!x&z|!y",
                    "!y|z",
                    "!x&z|!y|!z&x",
                    "!y|x|z",
                    "!(x|y&z)",
                    "!(x|y&z)|x&y&z",
                    "!x&!y|!z&y",
                    "!(x|y&z)|x&y",
                    "!x&!z|!y&z",
                    "!(x|y&z)|x&z",
                    "!(!y&!z&x|y&z)",
                    "!x&!y|!z&y|x&z",
                    "!((x|y)&z|x&y)",
                    "!((x|y)&z|x&y)|x&y&z",
                    "!x&!y|!z",
                    "!x&!y|!z|x&y",
                    "!x&!z|!y",
                    "!x&!z|!y|x&z",
                    "!y|!z",
                    "!y|!z|x",
                    "!x",
                    "!x|y&z",
                    "!x|!z&y",
                    "!x|y",
                    "!x|!y&z",
                    "!x|z",
                    "!x|!y&z|!z&y",
                    "!x|y|z",
                    "!x|!y&!z",
                    "!x|!y&!z|y&z",
                    "!x|!z",
                    "!x|!z|y",
                    "!x|!y",
                    "!x|!y|z",
                    "!(x&y&z)",
                    "!x|x"};
int main() {
  read(n, 10);
  while (n--) {
    read(a, 2);
    printf("%s\n", ch[a]);
  }
}
