#include <bits/stdc++.h>
const int N = 1e5 + 1;
using namespace std;
struct node {
  string s;
  int msk, tp;
  bool operator<(const node &i) const {
    if (s.size() > i.s.size()) return true;
    if (s.size() < i.s.size()) return false;
    return s > i.s;
  }
};
string add(string a, string b) {
  a += b;
  return a;
}
string add(string a, string b, string c) {
  a += b;
  a += c;
  return a;
}
string best(string a, string b) {
  if (a.size() < b.size()) return a;
  if (a.size() > b.size()) return b;
  return min(a, b);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int p[8]{1, 2, 4, 8, 16, 32, 64, 128};
  string ans[256][2];
  priority_queue<node> q;
  int x = p[4] + p[5] + p[6] + p[7], y = p[2] + p[3] + p[6] + p[7],
      z = p[1] + p[3] + p[5] + p[7];
  int all = (1 << 8) - 1;
  q.push({"x", x, 0});
  q.push({"y", y, 0});
  q.push({"z", z, 0});
  q.push({"!x", x ^ all, 0});
  q.push({"!y", y ^ all, 0});
  q.push({"!z", z ^ all, 0});
  while (!q.empty()) {
    node u = q.top();
    q.pop();
    if (!ans[u.msk][u.tp].empty()) continue;
    ans[u.msk][u.tp] = u.s;
    if (u.tp == 1) q.push({add("(", u.s, ")"), u.msk, 0});
    if (u.tp == 0) q.push({u.s, u.msk, 1});
    q.push({add("!", add("(", u.s, ")")), u.msk ^ all, 0});
    for (int i = 0; i < (1 << 8); i++) {
      if (!u.tp and !ans[i][0].empty()) {
        q.push({add(ans[i][0], "&", u.s), i & u.msk, 0});
        q.push({add(u.s, "&", ans[i][0]), i & u.msk, 0});
      }
      if (!ans[i][1].empty()) {
        q.push({add(ans[i][1], "|", u.s), i | u.msk, 1});
        q.push({add(u.s, "|", ans[i][1]), i | u.msk, 1});
      }
    }
  }
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    int msk = 0;
    for (int i = 0; i < 8; i++) msk += (1 << i) * (s[i] == '1');
    cout << ans[msk][1] << '\n';
  }
}
