#include <bits/stdc++.h>
using namespace std;
const int x = 240, y = 204, z = 170;
bool vis[260][3];
string f[260][3];
inline void update(string &a, string b) {
  bool flag = a.length() == b.length() ? a < b : a.length() < b.length();
  if (!flag || !a.length()) a = b;
}
int main() {
  f[x][2] = "x", f[y][2] = "y", f[z][2] = "z";
  f[255 ^ x][2] = "!x", f[255 ^ y][2] = "!y", f[255 ^ z][2] = "!z";
  while (1) {
    int s = -1, a = -1;
    string res;
    for (int st = 0; st < 256; st++)
      for (int at = 0; at < 3; at++) {
        if (!f[st][at].length() || vis[st][at]) continue;
        update(res, f[st][at]);
        if (res == f[st][at]) s = st, a = at;
      }
    if (s == -1 && a == -1) break;
    vis[s][a] = true;
    update(f[255 ^ s][2], "!(" + f[s][a] + ")");
    string ns;
    if (!a)
      ns = '(' + f[s][a] + ')';
    else
      ns = f[s][a];
    for (int t = 0; t < 256; t++)
      for (int b = 0; b < 3; b++) {
        if (!f[t][b].length()) continue;
        update(f[s | t][0], f[s][a] + '|' + f[t][b]);
        update(f[s | t][0], f[t][b] + '|' + f[s][a]);
        string nt;
        if (!b)
          nt = '(' + f[t][b] + ')';
        else
          nt = f[t][b];
        update(f[s & t][1], ns + '&' + nt);
        update(f[s & t][1], nt + '&' + ns);
      }
  }
  int T;
  scanf("%d", &T);
  while (T--) {
    char s[10];
    scanf("%s", s);
    int now = 0;
    for (int i = 0; i < 8; i++)
      if (s[i] == '1') now |= (1 << i);
    string ans;
    for (int i = 0; i < 3; i++) update(ans, f[now][i]);
    cout << ans << endl;
  }
  return 0;
}
