#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000001;
const long long LINF = 1000000000000000001LL;
const int M = 1 << 8;
const int T = 3;
string expr[T][M];
bool smaller(const string &a, const string &b) {
  if (a.size() != b.size()) return a.size() < b.size();
  return a < b;
}
bool minify(int t, int mask, const string &a) {
  if (a.empty()) return false;
  if (expr[t][mask].empty() || smaller(a, expr[t][mask])) {
    expr[t][mask] = a;
    return true;
  }
  return false;
}
int neg(int mask) { return mask ^ (M - 1); }
void pre() {
  string init[] = {"z", "y", "x"};
  for (int i = (0); i <= (2); ++i) {
    int mask = 0;
    for (int j = (0); j <= (7); ++j) {
      int value = (j >> i) % 2;
      if (value) {
        mask |= (1 << j);
      }
    }
    minify(2, mask, init[i]);
  }
  bool changed;
  do {
    changed = false;
    for (int t = (1); t <= (2); ++t)
      for (int mask = (0); mask <= (M - 1); ++mask) {
        changed |= minify(t - 1, mask, expr[t][mask]);
      }
    for (int mask = (0); mask <= (M - 1); ++mask)
      if (!expr[2][mask].empty()) {
        changed |= minify(2, neg(mask), "!" + expr[2][mask]);
      }
    for (int mask = (0); mask <= (M - 1); ++mask)
      if (!expr[0][mask].empty()) {
        changed |= minify(2, mask, "(" + expr[0][mask] + ")");
      }
    for (int mask = (0); mask <= (M - 1); ++mask)
      if (!expr[1][mask].empty()) {
        for (int mask2 = (0); mask2 <= (M - 1); ++mask2)
          if (!expr[2][mask2].empty()) {
            changed |=
                minify(1, mask & mask2, expr[1][mask] + "&" + expr[2][mask2]);
          }
      }
    for (int mask = (0); mask <= (M - 1); ++mask)
      if (!expr[0][mask].empty()) {
        for (int mask2 = (0); mask2 <= (M - 1); ++mask2)
          if (!expr[1][mask2].empty()) {
            changed |=
                minify(0, mask | mask2, expr[0][mask] + "|" + expr[1][mask2]);
          }
      }
  } while (changed);
}
int main() {
  ios_base::sync_with_stdio(0);
  pre();
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    int mask = 0;
    for (int i = (0); i <= (7); ++i)
      if (s[i] == '1') {
        mask |= (1 << i);
      }
    cout << expr[0][mask] << '\n';
  }
  return 0;
}
