#include <bits/stdc++.h>
using namespace std;
int val[305][3];
string str[305][3];
int t;
priority_queue<pair<int, pair<int, int> > > pq;
bool operator<(const string &a, const string &b) {
  if (a.size() == b.size())
    for (int i = 0; i < (int)a.size(); i++) {
      if (a[i] < b[i]) return 1;
      if (b[i] < a[i]) return 0;
    }
  return a.size() < b.size();
}
void init() {
  val[15][0] = 1;
  str[15][0] = "x";
  val[51][0] = 1;
  str[51][0] = "y";
  val[85][0] = 1;
  str[85][0] = "z";
  pq.push(make_pair(-1, make_pair(15, 0)));
  pq.push(make_pair(-1, make_pair(51, 0)));
  pq.push(make_pair(-1, make_pair(85, 0)));
}
void go_not(int tab, int out) {
  int tab2 = 255 - tab, out2 = 0;
  string nows = "!";
  nows += ((out > 0) ? "(" : "") + str[tab][out] + ((out > 0) ? ")" : "");
  if (!val[tab2][out2] || nows < str[tab2][out2]) {
    val[tab2][out2] = (int)nows.size();
    str[tab2][out2] = nows;
    pq.push(make_pair(-val[tab2][out2], make_pair(tab2, out2)));
  }
}
void go_and(int tab, int out) {
  string nows;
  int tab2, out2;
  vector<pair<pair<int, int>, int> > add;
  for (int tab3 = 0; tab3 <= 255; tab3++)
    for (int out3 = 0; out3 <= 2; out3++)
      if (val[tab3][out3]) {
        for (int i = 0; i < 2; i++) {
          tab2 = tab & tab3;
          out2 = 1;
          nows = ((out > 1) ? "(" : "") + str[tab][out] +
                 ((out > 1) ? ")" : "") + "&" + ((out3 >= 1) ? "(" : "") +
                 str[tab3][out3] + ((out3 >= 1) ? ")" : "");
          if (!str[tab2][out2].size() || nows < str[tab2][out2]) {
            add.push_back(make_pair(make_pair(tab2, out2), nows.size()));
            str[tab2][out2] = nows;
            pq.push(make_pair(-nows.size(), make_pair(tab2, out2)));
          }
          swap(tab, tab3);
          swap(out, out3);
        }
      }
  for (int i = 0; i < (int)add.size(); i++)
    val[add[i].first.first][add[i].first.second] = add[i].second;
}
void go_or(int tab, int out) {
  int tab2, out2;
  string nows;
  vector<pair<pair<int, int>, int> > add;
  for (int tab3 = 0; tab3 <= 255; tab3++)
    for (int out3 = 0; out3 <= 2; out3++)
      if (val[tab3][out3]) {
        for (int i = 0; i < 2; i++) {
          tab2 = tab | tab3;
          out2 = 2;
          nows = str[tab][out] + "|" + ((out3 >= 2) ? "(" : "") +
                 str[tab3][out3] + ((out3 >= 2) ? ")" : "");
          if (!str[tab2][out2].size() || nows < str[tab2][out2]) {
            add.push_back(make_pair(make_pair(tab2, out2), nows.size()));
            str[tab2][out2] = nows;
            pq.push(make_pair(-nows.size(), make_pair(tab2, out2)));
          }
          swap(tab, tab3);
          swap(out, out3);
        }
      }
  for (int i = 0; i < (int)add.size(); i++)
    val[add[i].first.first][add[i].first.second] = add[i].second;
}
void go() {
  init();
  while (!pq.empty()) {
    if (val[pq.top().second.first][pq.top().second.second] != -pq.top().first) {
      pq.pop();
      continue;
    }
    pair<int, int> now = pq.top().second;
    pq.pop();
    go_not(now.first, now.second);
    go_and(now.first, now.second);
    go_or(now.first, now.second);
  }
}
int main() {
  scanf("%d", &t);
  go();
  while (t--) {
    string s;
    cin >> s;
    int va = 0;
    for (int i = 0; i < 8; i++) va += pow(2, 7 - i) * (s[i] - '0');
    string now;
    for (int i = 1; i <= 100; i++) now += "z";
    for (int i = 0; i < 3; i++)
      if (str[va][i].size() && str[va][i] < now) now = str[va][i];
    puts(now.c_str());
  }
  return 0;
}
