#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int typ[1000000];
int val[1000000];
const int N = 256;
string dp[3][N];
int st = 0, ed = 0;
void assign(int tp, int vl, string s) {
  if (dp[tp][vl] != "" && ((int)(dp[tp][vl].size()) < (int)(s.size()))) return;
  if ((int)(dp[tp][vl].size()) == (int)(s.size()) && s > dp[tp][vl]) return;
  dp[tp][vl] = s;
  ed++;
  typ[ed] = tp;
  val[ed] = vl;
}
void bfs() {
  ed = 2;
  typ[0] = typ[1] = typ[2] = 2;
  dp[2][240] = "x";
  dp[2][170] = "z";
  dp[2][204] = "y";
  val[0] = 240;
  val[1] = 170;
  val[2] = 204;
  while (st <= ed) {
    if (typ[st] == 0) {
      int vl = val[st];
      for (int i = 0; i < N; i++) {
        if (dp[1][i] != "") {
          assign(0, i | vl, dp[0][vl] + "|" + dp[1][i]);
        }
      }
      assign(2, vl, "(" + dp[0][vl] + ")");
    } else if (typ[st] == 1) {
      int vl = val[st];
      for (int i = 0; i < N; i++) {
        if (dp[0][i] != "") {
          assign(0, i | vl, dp[0][i] + "|" + dp[1][vl]);
        }
        if (dp[2][i] != "") {
          assign(1, i & vl, dp[1][vl] + "&" + dp[2][i]);
        }
      }
      assign(0, vl, dp[1][vl]);
    } else {
      int vl = val[st];
      for (int i = 0; i < N; i++) {
        if (dp[1][i] != "") {
          assign(1, i & vl, dp[1][i] + "&" + dp[2][vl]);
        }
      }
      assign(1, vl, dp[2][vl]);
      assign(2, 255 - vl, "!" + dp[2][vl]);
    }
    st++;
  }
}
int main() {
  bfs();
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int pw = 1, curr = 0;
    for (int i = 0; i < 8; i++) {
      if (s[i] == '1') curr += pw;
      pw *= 2;
    }
    cout << dp[0][curr] << "\n";
  }
  return 0;
}
