#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
struct expr {
  string s;
  int x, op;
  bool friend operator<(expr a, expr b) {
    return make_pair(a.s.size(), a.s) > make_pair(b.s.size(), b.s);
  }
};
priority_queue<expr> q;
string ans[256][3];
char ch[25];
int vis[256][3];
int main() {
  q.push((expr){"x", 240, 2});
  q.push((expr){"y", 204, 2});
  q.push((expr){"z", 170, 2});
  while (!q.empty()) {
    expr u = q.top();
    q.pop();
    if (vis[u.x][u.op]) continue;
    vis[u.x][u.op] = 1;
    ans[u.x][u.op] = u.s;
    if (u.op == 0) {
      q.push((expr){"(" + u.s + ")", u.x, 2});
      for (int i = 0; i <= 255; i++) {
        if (vis[i][1]) q.push((expr){u.s + "|" + ans[i][1], u.x | i, 0});
      }
    }
    if (u.op == 1) {
      q.push((expr){u.s, u.x, 0});
      for (int i = 0; i <= 255; i++) {
        if (vis[i][2]) q.push((expr){u.s + "&" + ans[i][2], u.x & i, 1});
        if (vis[i][0]) q.push((expr){ans[i][0] + "|" + u.s, u.x | i, 0});
      }
    }
    if (u.op == 2) {
      q.push((expr){"!" + u.s, 255 ^ u.x, 2});
      q.push((expr){u.s, u.x, 1});
      for (int i = 0; i <= 255; i++)
        if (vis[i][1]) {
          q.push((expr){ans[i][1] + "&" + u.s, u.x & i, 1});
        }
    }
  }
  int n = read();
  while (n--) {
    scanf("%s", ch);
    int sum = 0;
    for (int i = 7; i >= 0; i--) sum = sum * 2 + (ch[i] - '0');
    cout << ans[sum][0] << endl;
  }
  return 0;
}
