#include <bits/stdc++.h>
using namespace std;
int read() {
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = (xx << 3) + (xx << 1) + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
const int X = 15, Y = 51, Z = 85;
string E[256], T[256], F[256];
bool flag;
void upd(string &A, const string &B) {
  if (A == "") {
    A = B;
    flag = 1;
    return;
  }
  int t1 = A.size(), t2 = B.size();
  if (t1 > t2)
    A = B, flag = 1;
  else if (t1 == t2)
    if (A > B) A = B, flag = 1;
}
int main() {
  F[X] = 'x', F[Y] = 'y', F[Z] = 'z';
  while (1) {
    flag = 0;
    for (int i = 0; i <= 255; i++)
      if (T[i] != "")
        for (int j = 0; j <= 255; j++)
          if (F[j] != "") upd(T[i & j], T[i] + '&' + F[j]);
    for (int i = 0; i <= 255; i++)
      if (E[i] != "")
        for (int j = 0; j <= 255; j++)
          if (T[j] != "") upd(E[i | j], E[i] + '|' + T[j]);
    for (int i = 0; i <= 255; i++)
      if (E[i] != "") upd(F[i], '(' + E[i] + ')');
    for (int i = 0; i <= 255; i++)
      if (F[i] != "") upd(T[i], F[i]);
    for (int i = 0; i <= 255; i++)
      if (T[i] != "") upd(E[i], T[i]);
    for (int i = 0; i <= 255; i++)
      if (F[i] != "") upd(F[i ^ 255], '!' + F[i]);
    if (!flag) break;
  }
  int T, N;
  char s[10];
  T = read();
  while (T--) {
    gets(s);
    N = 0;
    for (int i = 0; i <= 7; i++) N = (N << 1) + s[i] - '0';
    cout << E[N] << endl;
  }
  return 0;
}
