#include <bits/stdc++.h>
using namespace std;
const int N = 300;
string F[N], T[N], E[N];
bool flag;
void modify(string &A, const string &B) {
  if (A == "")
    A = B, flag = 1;
  else if (A.size() > B.size())
    A = B, flag = 1;
  else if (A.size() == B.size())
    if (A > B) A = B, flag = 1;
}
void init() {
  int X = 15, Y = 51, Z = 85;
  F[X] = "x", F[Y] = "y", F[Z] = "z";
  while (1) {
    flag = false;
    for (int i = 0; i < 256; i++) {
      if (T[i] == "") continue;
      for (int j = 0; j < 256; j++) {
        if (F[j] == "") continue;
        modify(T[i & j], T[i] + '&' + F[j]);
      }
    }
    for (int i = 0; i < 256; i++) {
      if (E[i] == "") continue;
      for (int j = 0; j < 256; j++) {
        if (T[j] == "") continue;
        modify(E[i | j], E[i] + '|' + T[j]);
      }
    }
    for (int i = 0; i < 256; i++)
      if (E[i] != "") modify(F[i], '(' + E[i] + ')');
    for (int i = 0; i < 256; i++)
      if (F[i] != "") modify(T[i], F[i]);
    for (int i = 0; i < 256; i++)
      if (T[i] != "") modify(E[i], T[i]);
    for (int i = 0; i < 256; i++)
      if (F[i] != "") modify(F[i ^ 255], '!' + F[i]);
    if (!flag) break;
  }
}
int main() {
  init();
  int n;
  scanf("%d", &n);
  while (n--) {
    int cur = 0;
    for (int i = 7; i >= 0; i--) {
      int x;
      scanf("%1d", &x);
      cur += x << i;
    }
    cout << E[cur] << endl;
  }
  return 0;
}
