#include <bits/stdc++.h>
using namespace std;
typedef set<pair<int, int>, function<bool(pair<int, int>, pair<int, int>)>>
    MOJQ;
const int xMask = 0b00001111;
const int yMask = 0b00110011;
const int zMask = 0b01010101;
enum Type { AND, OR };
string sols[256][2];
const auto cmp = [](pair<int, int> a, pair<int, int> b) -> bool {
  string s1 = sols[a.first][a.second];
  string s2 = sols[b.first][b.second];
  if (s1.size() == 0 && s2.size() == 0) {
    if (a.first == b.first) return a.second < b.second;
    return a.first < b.first;
  }
  if (s2.size() == 0) return true;
  if (s1.size() == 0) return false;
  if (s1.size() == s2.size()) return s1 <= s2;
  return s1.size() < s2.size();
};
int negateMask(int mask) { return 0b11111111 ^ mask; }
string surround(string s) { return "(" + s + ")"; }
string combineNegate(string s) {
  if (s == "") return "";
  if (s.size() > 1) s = surround(s);
  return "!" + s;
}
string combine(int mask1, int type1, int mask2, int type2, int type) {
  string s1 = sols[mask1][type1];
  string s2 = sols[mask2][type2];
  if (s1 == "") return s2;
  if (s2 == "") return s1;
  if (type == OR) {
    if (s1 > s2) swap(s1, s2);
    return s1 + "|" + s2;
  } else {
    if (type1 == OR) s1 = surround(s1);
    if (type2 == OR) s2 = surround(s2);
    if (s1 > s2) swap(s1, s2);
    return s1 + "&" + s2;
  }
}
bool bio[256][2];
void update(int mask, int type, string newString, MOJQ& Q) {
  if (newString == "") return;
  string oldString = sols[mask][type];
  if (oldString == "" || oldString.size() > newString.size() ||
      oldString.size() == newString.size() && newString < oldString) {
    sols[mask][type] = newString;
    Q.erase({mask, type});
    Q.emplace(mask, type);
  }
}
void generate() {
  for (int i = 0; i < 256; i++)
    for (int t = 0; t < 2; t++) sols[i][t] = "";
  sols[xMask][AND] = sols[xMask][OR] = "x";
  sols[yMask][AND] = sols[yMask][OR] = "y";
  sols[zMask][AND] = sols[zMask][OR] = "z";
  MOJQ Q(cmp);
  for (int mask = 0; mask < 256; mask++)
    for (int type = 0; type < 2; type++) Q.emplace(mask, type);
  while (!Q.empty()) {
    auto [mask, type] = *Q.begin();
    Q.erase(Q.begin());
    if (bio[mask][type]) continue;
    string cur = sols[mask][type];
    bio[mask][type] = true;
    update(negateMask(mask), AND, combineNegate(cur), Q);
    update(negateMask(mask), OR, combineNegate(cur), Q);
    for (int mask_ = 0; mask_ < 256; mask_++)
      for (int type_ = 0; type_ < 2; type_++) {
        if (!bio[mask_][type_]) continue;
        if (mask == mask_ && type == type_) continue;
        if (sols[mask_][type_] == "") continue;
        update(mask & mask_, AND, combine(mask, type, mask_, type_, AND), Q);
        update(mask | mask_, OR, combine(mask, type, mask_, type_, OR), Q);
      }
  }
  for (int mask = 0; mask < 256; mask++) {
    string sol =
        cmp({mask, AND}, {mask, OR}) ? sols[mask][AND] : sols[mask][OR];
    sols[mask][0] = sol;
  }
}
int n;
void load() { scanf("%d", &n); }
void solve() {
  generate();
  char s[9];
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    int mask = 0;
    for (int j = 0; j < 8; j++) mask = (mask << 1) | (s[j] - '0');
    printf("%s\n", sols[mask][0].c_str());
  }
}
int main() {
  load();
  solve();
  return 0;
}
