#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300;
string E[MAXN], T[MAXN], F[MAXN];
bool cmp(string& a, string& b) {
  return a.size() != b.size() ? a.size() < b.size() : a < b;
}
bool update(string& a, string b) { return a == "" || cmp(b, a) ? a = b, 1 : 0; }
void init() {
  F[240] = "x";
  F[204] = "y";
  F[170] = "z";
  while (1) {
    bool ok = false;
    for (int i = 0; i < 256; i++) {
      if (E[i] != "") ok |= update(F[i], "(" + E[i] + ")");
    }
    for (int i = 0; i < 256; i++) {
      if (F[i] != "") ok |= update(F[i ^ 255], "!" + F[i]);
    }
    for (int i = 0; i < 256; i++) {
      if (F[i] != "") ok |= update(T[i], F[i]);
    }
    for (int i = 0; i < 256; i++) {
      if (T[i] != "") {
        for (int j = 0; j < 256; j++) {
          if (T[j] != "") {
            ok |= update(T[i & j], T[i] + "&" + T[j]);
          }
        }
      }
    }
    for (int i = 0; i < 256; i++) {
      if (T[i] != "") {
        ok |= update(E[i], T[i]);
      }
    }
    for (int i = 0; i < 256; i++) {
      if (E[i] != "") {
        for (int j = 0; j < 256; j++) {
          if (E[j] != "") {
            ok |= update(E[i | j], E[i] + "|" + E[j]);
          }
        }
      }
    }
    if (!ok) break;
  }
}
char s[10];
int main() {
  int T;
  init();
  scanf("%d", &T);
  while (T--) {
    int x = 0;
    scanf("%s", s);
    for (int i = 0; i < 8; i++) {
      x |= (s[i] - '0') << i;
    }
    cout << E[x] << endl;
  }
  return 0;
}
