#include <bits/stdc++.h>
using namespace std;
string dp[300][5];
vector<int> opr[5];
string mini(string a, string b) {
  if (a.empty()) return b;
  if (b.empty()) return a;
  if (a.size() < b.size()) return a;
  if (b.size() < a.size()) return b;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] < b[i]) return a;
    if (b[i] < a[i]) return b;
  }
  return a;
}
int main() {
  dp[15][2] = "x";
  dp[51][2] = "y";
  dp[85][2] = "z";
  opr[2].push_back(15);
  opr[2].push_back(51);
  opr[2].push_back(85);
  priority_queue<pair<pair<int, string>, pair<int, int> >,
                 vector<pair<pair<int, string>, pair<int, int> > >,
                 greater<pair<pair<int, string>, pair<int, int> > > >
      pq;
  pq.push(make_pair(make_pair(1, "x"), make_pair(15, 2)));
  pq.push(make_pair(make_pair(1, "y"), make_pair(51, 2)));
  pq.push(make_pair(make_pair(1, "z"), make_pair(85, 2)));
  while (!pq.empty()) {
    pair<pair<int, string>, pair<int, int> > top = pq.top();
    pq.pop();
    int len = top.first.first;
    string str = top.first.second;
    int bit = top.second.first;
    int id = top.second.second;
    if (dp[bit][id] != str) continue;
    string tmp;
    if (id == 0) {
      for (int a = 0; a < opr[1].size(); a++) {
        tmp = mini(dp[bit | opr[1][a]][0], str + '|' + dp[opr[1][a]][1]);
        if (tmp != dp[bit | opr[1][a]][0]) {
          dp[bit | opr[1][a]][0] = tmp;
          opr[0].push_back(bit | opr[1][a]);
          pq.push(make_pair(make_pair(tmp.size(), tmp),
                            make_pair(bit | opr[1][a], 0)));
        }
      }
      tmp = mini(dp[bit][2], '(' + str + ')');
      if (dp[bit][2] != tmp) {
        dp[bit][2] = tmp;
        opr[2].push_back(bit);
        pq.push(make_pair(make_pair(tmp.size(), tmp), make_pair(bit, 2)));
      }
    } else if (id == 1) {
      tmp = mini(dp[bit][0], str);
      if (dp[bit][0] != tmp) {
        dp[bit][0] = tmp;
        opr[0].push_back(bit);
        pq.push(make_pair(make_pair(tmp.size(), tmp), make_pair(bit, 0)));
      }
      for (int a = 0; a < opr[2].size(); a++) {
        tmp = mini(dp[bit & opr[2][a]][1], str + '&' + dp[opr[2][a]][2]);
        if (dp[bit & opr[2][a]][1] != tmp) {
          dp[bit & opr[2][a]][1] = tmp;
          opr[1].push_back(bit & opr[2][a]);
          pq.push(make_pair(make_pair(tmp.size(), tmp),
                            make_pair(bit & opr[2][a], 1)));
        }
      }
      for (int a = 0; a < opr[0].size(); a++) {
        tmp = mini(dp[opr[0][a] | bit][0], dp[opr[0][a]][0] + '|' + str);
        if (dp[opr[0][a] | bit][0] != tmp) {
          dp[opr[0][a] | bit][0] = tmp;
          opr[0].push_back(opr[0][a] | bit);
          pq.push(make_pair(make_pair(tmp.size(), tmp),
                            make_pair(opr[0][a] | bit, 0)));
        }
      }
    } else {
      tmp = mini(dp[bit ^ ((1 << 8) - 1)][2], '!' + str);
      if (dp[bit ^ ((1 << 8) - 1)][2] != tmp) {
        dp[bit ^ ((1 << 8) - 1)][2] = tmp;
        opr[2].push_back(bit ^ ((1 << 8) - 1));
        pq.push(make_pair(make_pair(tmp.size(), tmp),
                          make_pair(bit ^ ((1 << 8) - 1), 2)));
      }
      tmp = mini(dp[bit][1], str);
      if (dp[bit][1] != tmp) {
        dp[bit][1] = tmp;
        opr[1].push_back(bit);
        pq.push(make_pair(make_pair(tmp.size(), tmp), make_pair(bit, 1)));
      }
      for (int a = 0; a < opr[1].size(); a++) {
        tmp = mini(dp[bit & opr[1][a]][1], dp[opr[1][a]][1] + '&' + str);
        if (dp[bit & opr[1][a]][1] != tmp) {
          dp[bit & opr[1][a]][1] = tmp;
          opr[1].push_back(bit & opr[1][a]);
          pq.push(make_pair(make_pair(tmp.size(), tmp),
                            make_pair(bit & opr[1][a], 1)));
        }
      }
    }
  }
  int t, mask, idx;
  scanf("%d", &t);
  while (t--) {
    mask = 0;
    idx = 0;
    string inp;
    cin >> inp;
    for (int a = inp.size() - 1; a >= 0; a--) {
      if (inp[a] == '1') {
        mask |= (1 << idx);
      }
      idx++;
    }
    string ans = mini(mini(dp[mask][0], dp[mask][1]), dp[mask][2]);
    cout << ans << endl;
  }
}
