#include <bits/stdc++.h>
using namespace std;
const int GEN = 0;
const int CON = 1;
const int DIS = 2;
bool acmp(const string& l, const string& r) {
  if (l.empty()) {
    return false;
  }
  if (r.empty()) {
    return true;
  }
  return l.size() < r.size() || l.size() == r.size() && l < r;
}
struct Bf {
  int mask;
  int mode;
  Bf() : mask(0), mode(0) {}
  Bf(int ma, int mo) : mask(ma), mode(mo) {}
};
const int N = 1 << 8;
const int M = 3;
string dp[N][M];
bool operator<(const Bf& l, const Bf& r) {
  return acmp(dp[l.mask][l.mode], dp[r.mask][r.mode]) ||
         dp[l.mask][l.mode] == dp[r.mask][r.mode] &&
             (l.mask < r.mask || l.mask == r.mask && l.mode < r.mode);
}
string& getAnswer(const Bf& bf) { return dp[bf.mask][bf.mode]; }
void upgrade(set<Bf>& toAdd, const Bf& bf, const string& str) {
  set<Bf>::iterator it = toAdd.find(bf);
  if (it != toAdd.end() && acmp(str, getAnswer(*it))) {
    toAdd.erase(it);
    getAnswer(bf) = str;
    toAdd.insert(bf);
  }
}
void makeDp() {
  set<Bf> toAdd;
  vector<Bf> optimal;
  dp[240][0] = "x";
  dp[204][0] = "y";
  dp[170][0] = "z";
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      toAdd.insert(Bf(i, j));
    }
  }
  Bf cur;
  while (!toAdd.empty()) {
    cur = *(toAdd.begin());
    if (getAnswer(cur) == "") {
      break;
    }
    toAdd.erase(toAdd.begin());
    string curPart = getAnswer(cur);
    upgrade(toAdd, Bf(cur.mask, GEN), '(' + curPart + ')');
    if (cur.mode <= DIS) {
      for (const Bf& other : optimal) {
        if (other.mode > DIS) {
          continue;
        }
        upgrade(toAdd, Bf(cur.mask | other.mask, DIS),
                min(curPart + '|' + getAnswer(other),
                    getAnswer(other) + '|' + curPart));
      }
    }
    if (cur.mode <= CON) {
      for (const Bf& other : optimal) {
        if (other.mode > CON) {
          continue;
        }
        upgrade(toAdd, Bf(cur.mask & other.mask, CON),
                min(curPart + '&' + getAnswer(other),
                    getAnswer(other) + '&' + curPart));
      }
    }
    if (cur.mode <= GEN) {
      upgrade(toAdd, Bf(cur.mask ^ (N - 1), GEN), '!' + curPart);
    }
    optimal.push_back(cur);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  makeDp();
  int nn;
  cin >> nn;
  string str;
  for (int i = 0; i < nn; ++i) {
    cin >> str;
    int mask = 0;
    for (int j = 7; j >= 0; --j) {
      mask = (mask << 1) | (str[j] - '0');
    }
    string res = "";
    for (int i = 0; i < M; ++i) {
      if (acmp(getAnswer(Bf(mask, i)), res)) {
        res = getAnswer(Bf(mask, i));
      }
    }
    cout << res << '\n';
  }
  return 0;
}
