#include <bits/stdc++.h>
using namespace std;
const int N = 256;
const int fullmask = 256 - 1;
const int EE = 0, FF = 1, TT = 2;
string a[3][N];
string ans[N];
int used[3][N];
const bool operator<(const string &x, const string &y) {
  return x.length() < y.length() ||
         x.length() == y.length() && strcmp(x.c_str(), y.c_str()) < 0;
}
void getans() {
  a[FF][0xf] = "x";
  a[FF][0x33] = "y";
  a[FF][0x55] = "z";
  for (int count = 3; count < 3 * N; ++count) {
    int p = -1, q = -1;
    for (int j = 0; j < 3; ++j) {
      for (int i = 0; i < N; ++i) {
        if (!used[j][i] && a[j][i].length() > 0) {
          if (p == -1 || a[j][i] < a[p][q]) {
            p = j, q = i;
          }
        }
      }
    }
    used[p][q] = 1;
    if (p == EE) {
      if (a[FF][q].length() == 0 || "(" + a[EE][q] + ")" < a[FF][q]) {
        a[FF][q] = "(" + a[EE][q] + ")";
      }
      for (int i = 0; i < 256; ++i)
        if (used[TT][i]) {
          if (a[EE][q | i].length() == 0 ||
              a[EE][q] + "|" + a[TT][i] < a[EE][q | i]) {
            a[EE][q | i] = a[EE][q] + "|" + a[TT][i];
          }
        }
    } else if (p == FF) {
      if (a[FF][q ^ fullmask].length() == 0 ||
          "!" + a[FF][q] < a[FF][q ^ fullmask]) {
        a[FF][q ^ fullmask] = "!" + a[FF][q];
      }
      if (a[TT][q].length() == 0 || a[FF][q] < a[TT][q]) {
        a[TT][q] = a[FF][q];
      }
      for (int i = 0; i < 256; ++i)
        if (used[TT][i]) {
          if (a[TT][i & q].length() == 0 ||
              a[TT][i] + "&" + a[FF][q] < a[TT][i & q]) {
            a[TT][i & q] = a[TT][i] + "&" + a[FF][q];
          }
        }
    } else if (p == TT) {
      if (a[EE][q].length() == 0 || a[TT][q] < a[EE][q]) {
        a[EE][q] = a[TT][q];
      }
      for (int i = 0; i < 256; ++i)
        if (used[FF][i]) {
          if (a[TT][q & i].length() == 0 ||
              a[TT][q] + "&" + a[FF][i] < a[TT][q & i]) {
            a[TT][q & i] = a[TT][q] + "&" + a[FF][i];
          }
        }
      for (int i = 0; i < 256; ++i)
        if (used[EE][i]) {
          if (a[EE][i | q].length() == 0 ||
              a[EE][i] + "|" + a[TT][q] < a[EE][i | q]) {
            a[EE][i | q] = a[EE][i] + "|" + a[TT][q];
          }
        }
    }
  }
  for (int j = 0; j < 256; ++j) {
    ans[j] = a[0][j];
    for (int i = 1; i < 3; ++i) {
      if (a[i][j] < ans[j]) {
        ans[j] = a[i][j];
      }
    }
  }
}
int main() {
  getans();
  int n;
  char s[10];
  scanf("%d", &n);
  while (n--) {
    scanf("%s", s);
    int t = strlen(s), c = 0;
    for (int i = 0; i < t; ++i) {
      c = c * 2 + s[i] - '0';
    }
    printf("%s\n", ans[c].c_str());
  }
}
