#include <bits/stdc++.h>
using namespace std;
const int N = 256;
string s[N][2], ans[N];
bool used[N][2];
char t[N];
queue<pair<int, int>> q;
bool cmp(string a, string b) {
  return a.size() < b.size() || a.size() == b.size() && a < b;
}
void upd(int x, int y, string val, bool rev = false) {
  if (!used[x][y] || cmp(val, s[x][y])) {
    used[x][y] = 1;
    s[x][y] = val;
    q.push({x, y});
    if (!y) upd(x, 1, "(" + val + ")");
    if (!rev) {
      upd(N - 1 - x, 1, "!(" + val + ")", true);
    }
  }
}
int main() {
  int a = 0;
  for (int i = 0; i < 8; i++) {
    if (i & 4) a |= 1 << i;
  }
  q.push({a, 1});
  s[a][1] = "x";
  used[a][1] = 1;
  used[N - 1 - a][1] = 1;
  s[N - 1 - a][1] = "!x";
  q.push({N - 1 - a, 1});
  a = 0;
  for (int i = 0; i < 8; i++) {
    if (i & 2) a |= 1 << i;
  }
  q.push({a, 1});
  s[a][1] = "y";
  used[a][1] = 1;
  used[N - 1 - a][1] = 1;
  s[N - 1 - a][1] = "!y";
  q.push({N - 1 - a, 1});
  a = 0;
  for (int i = 0; i < 8; i++) {
    if (i & 1) a |= 1 << i;
  }
  q.push({a, 1});
  s[a][1] = "z";
  used[a][1] = 1;
  used[N - 1 - a][1] = 1;
  s[N - 1 - a][1] = "!z";
  q.push({N - 1 - a, 1});
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < N; i++) {
      if (used[i][0]) {
        upd(i | x, 0, s[i][0] + "|" + s[x][y]);
        upd(i | x, 0, s[x][y] + "|" + s[i][0]);
      }
      if (used[i][1]) {
        if (y) {
          upd(i & x, 1, s[i][1] + "&" + s[x][y]);
          upd(i & x, 1, s[x][y] + "&" + s[i][1]);
        }
        upd(i | x, 0, s[i][1] + "|" + s[x][y]);
        upd(i | x, 0, s[x][y] + "|" + s[i][1]);
      }
    }
  }
  for (int i = 0; i < N; i++) {
    ans[i] = cmp(s[i][0], s[i][1]) ? s[i][0] : s[i][1];
  }
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf(" %s", t);
    int x = 0;
    for (int j = 0; j < 8; j++) {
      x |= (t[j] - '0') << j;
    }
    printf("%s\n", ans[x].c_str());
  }
  return 0;
}
