#include <bits/stdc++.h>
using namespace std;
const int N = 255;
int n, msk;
string dp[255 + 100][3], in, res;
bool changed = true;
inline string update(string &a, string b) {
  if (a == "" || b.size() < a.size() || (b.size() == a.size() && b < a)) {
    changed = true;
    a = b;
  }
  return a;
}
inline void init() {
  dp[(1 << 4) + (1 << 5) + (1 << 6) + (1 << 7)][0] = "x";
  dp[(1 << 2) + (1 << 3) + (1 << 6) + (1 << 7)][0] = "y";
  dp[(1 << 1) + (1 << 3) + (1 << 5) + (1 << 7)][0] = "z";
  while (changed) {
    changed = false;
    for (int i = 0; i < 256; i++) {
      for (int j = 0; j < 3; j++) {
        if (dp[i][j] == "") continue;
        string s = dp[i][j];
        if (j > 0) {
          s = "(" + s + ")";
        }
        s = "!" + s;
        update(dp[i ^ 255][j], s);
        for (int ii = 0; ii < 256; ii++) {
          for (int jj = 0; jj < 3; jj++) {
            if (dp[ii][jj] == "") continue;
            for (int op = 1; op <= 2; op++) {
              s = dp[i][j];
              if (j > op) {
                s = "(" + s + ")";
              }
              string t = dp[ii][jj];
              if (jj > op) {
                t = "(" + t + ")";
              }
              string nxt = s + (op == 1 ? "&" : "|") + t;
              update(dp[(op == 1 ? (i & ii) : (i | ii))][op], nxt);
            }
          }
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  init();
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> in;
    msk = 0;
    for (int i = 0; i < 8; i++) {
      int cur = in[i] - '0';
      if (cur) msk |= (1 << i);
    }
    res = "";
    for (int i = 0; i < 3; i++) {
      res = update(res, dp[msk][i]);
    }
    cout << res << "\n";
  }
  return 0;
}
