#include <bits/stdc++.h>
using namespace std;
const int mxn = 257;
string rec[mxn][3];
bool been[mxn][3];
inline int G(string s) {
  int t = 0;
  for (int i = 0; i < s.size(); ++i) t = t * 2 + s[i] - '0';
  return t;
}
priority_queue<pair<int, int> > pq;
bool changed;
string Min(string a, string b) {
  if (a.size() != b.size()) {
    if (a.size() < b.size())
      return a;
    else
      return b;
  }
  return min(a, b);
}
inline void upd(string& a, string b) {
  if (a == b) return;
  if (Min(a, b) == b) {
    changed = 1;
    a = b;
  }
}
inline void prep() {
  int n = 256;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < 3; ++j)
      rec[i][j] = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
  rec[G("00001111")][0] = "x";
  rec[G("00110011")][0] = "y";
  rec[G("01010101")][0] = "z";
  for (changed = 1; changed;) {
    changed = 0;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < 2; ++j) upd(rec[i][j + 1], rec[i][j]);
    for (int i = 0; i < n; ++i) upd(rec[i ^ 255][0], "!" + rec[i][0]);
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        upd(rec[i & j][1], rec[i][1] + "&" + rec[j][0]),
            upd(rec[i | j][2], rec[i][2] + "|" + rec[j][1]);
    for (int i = 0; i < n; ++i) upd(rec[i][0], "(" + rec[i][2] + ")");
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int T = 1;
  cin >> T;
  prep();
  for (; T--;) {
    string s;
    cin >> s;
    int t = G(s);
    cout << Min(Min(rec[t][0], rec[t][1]), rec[t][2]) << '\n';
  }
}
