#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> val;
map<pair<int, int>, string> str;
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[make_pair(15, 0)] = 1;
  str[make_pair(15, 0)] = "x";
  val[make_pair(51, 0)] = 1;
  str[make_pair(51, 0)] = "y";
  val[make_pair(85, 0)] = 1;
  str[make_pair(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(pair<int, int> now) {
  pair<int, int> now2 = now;
  now2.first = 255 - now.first;
  now2.second = 0;
  string nows = "!";
  nows +=
      ((now.second > 0) ? "(" : "") + str[now] + ((now.second > 0) ? ")" : "");
  if (val.find(now2) == val.end() || nows < str[now2]) {
    val[now2] = (int)nows.size();
    str[now2] = nows;
    pq.push(make_pair(-val[now2], now2));
  }
}
void go_and(pair<int, int> now) {
  pair<int, int> now2;
  string nows;
  vector<pair<pair<int, int>, int> > add;
  for (auto it = val.begin(); it != val.end(); it++) {
    pair<int, int> now3 = it->first;
    for (int i = 0; i < 2; i++) {
      now2 = make_pair(now.first & now3.first, 1);
      nows = ((now.second > 1) ? "(" : "") + str[now] +
             ((now.second > 1) ? ")" : "") + "&" +
             ((now3.second >= 1) ? "(" : "") + str[now3] +
             ((now3.second >= 1) ? ")" : "");
      if (str.find(now2) == str.end() || nows < str[now2]) {
        add.push_back(make_pair(now2, nows.size()));
        str[now2] = nows;
        pq.push(make_pair(-nows.size(), now2));
      }
      swap(now, now3);
    }
  }
  for (int i = 0; i < (int)add.size(); i++) val[add[i].first] = add[i].second;
}
void go_or(pair<int, int> now) {
  pair<int, int> now2;
  string nows;
  vector<pair<pair<int, int>, int> > add;
  for (auto it = val.begin(); it != val.end(); it++) {
    pair<int, int> now3 = it->first;
    for (int i = 0; i < 2; i++) {
      now2 = make_pair(now.first | now3.first, 2);
      nows = str[now] + "|" + ((now3.second >= 2) ? "(" : "") + str[now3] +
             ((now3.second >= 2) ? ")" : "");
      if (str.find(now2) == str.end() || nows < str[now2]) {
        add.push_back(make_pair(now2, nows.size()));
        str[now2] = nows;
        pq.push(make_pair(-nows.size(), now2));
      }
      swap(now, now3);
    }
  }
  for (int i = 0; i < (int)add.size(); i++) val[add[i].first] = add[i].second;
}
void go() {
  init();
  while (!pq.empty()) {
    if (val[pq.top().second] != -pq.top().first) {
      pq.pop();
      continue;
    }
    pair<int, int> now = pq.top().second;
    pq.pop();
    go_not(now);
    go_and(now);
    go_or(now);
  }
}
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.find(make_pair(va, i)) != str.end() &&
          str[make_pair(va, i)] < now)
        now = str[make_pair(va, i)];
    puts(now.c_str());
  }
  return 0;
}
