#include <bits/stdc++.h>
template <class C>
C& mini(C& a, C b) {
  if (b < a) a = b;
  return a;
}
template <class C>
C& maxi(C& a, C b) {
  if (a < b) a = b;
  return a;
}
using namespace std;
const char* dp[256] = {
    "!x&x",
    "!(x|y|z)",
    "!x&!y&z",
    "!x&!y",
    "!x&!z&y",
    "!x&!z",
    "!(!y&!z|x|y&z)",
    "!(x|y&z)",
    "!x&y&z",
    "!(!y&z|!z&y|x)",
    "!x&z",
    "!(!z&y|x)",
    "!x&y",
    "!(!y&z|x)",
    "!x&(y|z)",
    "!x",
    "!y&!z&x",
    "!y&!z",
    "!(!x&!z|x&z|y)",
    "!(x&z|y)",
    "!(!x&!y|x&y|z)",
    "!(x&y|z)",
    "!(!x&!y|x&y|z)|!x&!y&z",
    "!((x|y)&z|x&y)",
    "!x&y&z|!y&!z&x",
    "!x&y&z|!y&!z",
    "!x&z|!y&!z&x",
    "!x&z|!y&!z",
    "!x&y|!y&!z&x",
    "!x&y|!y&!z",
    "!x&(y|z)|!y&!z&x",
    "!x|!y&!z",
    "!y&x&z",
    "!(!x&z|!z&x|y)",
    "!y&z",
    "!(!z&x|y)",
    "!x&!z&y|!y&x&z",
    "!x&!z|!y&x&z",
    "!x&!z&y|!y&z",
    "!x&!z|!y&z",
    "!x&y&z|!y&x&z",
    "!(!x&z|!z&x|y)|!x&y&z",
    "!(x&y)&z",
    "!(!z&x|y)|!x&z",
    "!x&y|!y&x&z",
    "!(!y&z|x)|!y&x&z",
    "!x&y|!y&z",
    "!x|!y&z",
    "!y&x",
    "!(!x&z|y)",
    "!y&(x|z)",
    "!y",
    "!x&!z&y|!y&x",
    "!x&!z|!y&x",
    "!x&!z&y|!y&(x|z)",
    "!x&!z|!y",
    "!x&y&z|!y&x",
    "!(!x&z|y)|!x&y&z",
    "!x&z|!y&x",
    "!x&z|!y",
    "!x&y|!y&x",
    "!(!x&!y&z|x&y)",
    "!x&(y|z)|!y&x",
    "!x|!y",
    "!z&x&y",
    "!(!x&y|!y&x|z)",
    "!x&!y&z|!z&x&y",
    "!x&!y|!z&x&y",
    "!z&y",
    "!(!y&x|z)",
    "!x&!y&z|!z&y",
    "!x&!y|!z&y",
    "!x&y&z|!z&x&y",
    "!(!x&y|!y&x|z)|!x&y&z",
    "!x&z|!z&x&y",
    "!(!z&y|x)|!z&x&y",
    "!(x&z)&y",
    "!(!y&x|z)|!x&y",
    "!x&z|!z&y",
    "!x|!z&y",
    "!z&x",
    "!(!x&y|z)",
    "!x&!y&z|!z&x",
    "!x&!y|!z&x",
    "!z&(x|y)",
    "!z",
    "!x&!y&z|!z&(x|y)",
    "!x&!y|!z",
    "!x&y&z|!z&x",
    "!(!x&y|z)|!x&y&z",
    "!x&z|!z&x",
    "!(!x&!z&y|x&z)",
    "!x&y|!z&x",
    "!x&y|!z",
    "!x&(y|z)|!z&x",
    "!x|!z",
    "!y&x&z|!z&x&y",
    "!(!x&y|!y&x|z)|!y&x&z",
    "!y&z|!z&x&y",
    "!(!z&x|y)|!z&x&y",
    "!y&x&z|!z&y",
    "!(!y&x|z)|!y&x&z",
    "!y&z|!z&y",
    "!(!y&!z&x|y&z)",
    "!x&y&z|!y&x&z|!z&x&y",
    "!(!x&y|!y&x|z)|!x&y&z|!y&x&z",
    "!(x&y)&z|!z&x&y",
    "!(!z&x|y)|!x&z|!z&x&y",
    "!(x&z)&y|!y&x&z",
    "!(!y&x|z)|!x&y|!y&x&z",
    "!(x&y)&z|!z&y",
    "!x|!y&z|!z&y",
    "!(y&z)&x",
    "!(!x&y|z)|!y&x",
    "!y&z|!z&x",
    "!y|!z&x",
    "!y&x|!z&y",
    "!y&x|!z",
    "!y&(x|z)|!z&y",
    "!y|!z",
    "!(y&z)&x|!x&y&z",
    "!(!x&y|z)|!x&y&z|!y&x",
    "!(x&y)&z|!z&x",
    "!x&z|!y|!z&x",
    "!(x&z)&y|!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&z|x&y&z",
    "!x&!y|x&y&z",
    "!x&!z&y|x&y&z",
    "!x&!z|x&y&z",
    "!(!y&!z|x|y&z)|x&y&z",
    "!(x|y&z)|x&y&z",
    "y&z",
    "!(x|y|z)|y&z",
    "!x&z|y&z",
    "!x&!y|y&z",
    "!x&y|y&z",
    "!x&!z|y&z",
    "!x&(y|z)|y&z",
    "!x|y&z",
    "!y&!z&x|x&y&z",
    "!y&!z|x&y&z",
    "!(!x&!z|x&z|y)|x&y&z",
    "!(x&z|y)|x&y&z",
    "!(!x&!y|x&y|z)|x&y&z",
    "!(x&y|z)|x&y&z",
    "!(!x&!y|x&y|z)|!x&!y&z|x&y&z",
    "!((x|y)&z|x&y)|x&y&z",
    "!y&!z&x|y&z",
    "!y&!z|y&z",
    "!x&z|!y&!z&x|y&z",
    "!(x&z|y)|y&z",
    "!x&y|!y&!z&x|y&z",
    "!(x&y|z)|y&z",
    "!x&(y|z)|!y&!z&x|y&z",
    "!x|!y&!z|y&z",
    "x&z",
    "!(x|y|z)|x&z",
    "!y&z|x&z",
    "!x&!y|x&z",
    "!x&!z&y|x&z",
    "!x&!z|x&z",
    "!x&!z&y|!y&z|x&z",
    "!(x|y&z)|x&z",
    "(x|y)&z",
    "!(x|y|z)|(x|y)&z",
    "z",
    "!x&!y|z",
    "!x&y|x&z",
    "!(!y&z|x)|x&z",
    "!x&y|z",
    "!x|z",
    "!y&x|x&z",
    "!y&!z|x&z",
    "!y&(x|z)|x&z",
    "!y|x&z",
    "!x&!z&y|!y&x|x&z",
    "!(x&y|z)|x&z",
    "!x&!z&y|!y&(x|z)|x&z",
    "!x&!z|!y|x&z",
    "!y&x|y&z",
    "!(!x&z|y)|y&z",
    "!y&x|z",
    "!y|z",
    "!x&y|!y&x|x&z",
    "!x&!z|!y&x|y&z",
    "!x&y|!y&x|z",
    "!x|!y|z",
    "x&y",
    "!(x|y|z)|x&y",
    "!x&!y&z|x&y",
    "!x&!y|x&y",
    "!z&y|x&y",
    "!x&!z|x&y",
    "!x&!y&z|!z&y|x&y",
    "!(x|y&z)|x&y",
    "(x|z)&y",
    "!(x|y|z)|(x|z)&y",
    "!x&z|x&y",
    "!(!z&y|x)|x&y",
    "y",
    "!x&!z|y",
    "!x&z|y",
    "!x|y",
    "!z&x|x&y",
    "!y&!z|x&y",
    "!x&!y&z|!z&x|x&y",
    "!(x&z|y)|x&y",
    "!z&(x|y)|x&y",
    "!z|x&y",
    "!x&!y&z|!z&(x|y)|x&y",
    "!x&!y|!z|x&y",
    "!z&x|y&z",
    "!(!x&y|z)|y&z",
    "!x&z|!z&x|x&y",
    "!x&!y|!z&x|y&z",
    "!z&x|y",
    "!z|y",
    "!x&z|!z&x|y",
    "!x|!z|y",
    "(y|z)&x",
    "!(x|y|z)|(y|z)&x",
    "!y&z|x&y",
    "!(!z&x|y)|x&y",
    "!z&y|x&z",
    "!(!y&x|z)|x&z",
    "!y&z|!z&y|x&y",
    "!x&!y|!z&y|x&z",
    "(x|y)&z|x&y",
    "!(x|y|z)|(x|y)&z|x&y",
    "x&y|z",
    "!x&!y|x&y|z",
    "x&z|y",
    "!x&!z|x&z|y",
    "y|z",
    "!x|y|z",
    "x",
    "!y&!z|x",
    "!y&z|x",
    "!y|x",
    "!z&y|x",
    "!z|x",
    "!y&z|!z&y|x",
    "!y|!z|x",
    "x|y&z",
    "!y&!z|x|y&z",
    "x|z",
    "!y|x|z",
    "x|y",
    "!z|x|y",
    "x|y|z",
    "!x|x",
};
int main(int argc, const char* argv[]) {
  int N;
  scanf("%d\n", &N);
  while (N-- > 0) {
    unsigned char key = 0;
    for (int i = 0; i < 8; i++) {
      char c;
      scanf("%c", &c);
      if (c == '1') key |= (1 << i);
    }
    scanf("\n");
    printf("%s\n", dp[key]);
  }
  return 0;
}
