#include <bits/stdc++.h>
using namespace std;
string f[21][256][3];
int n;
string ans;
char st[10];
int main() {
  int i, j1, k1, j2, k2;
  f[0][15][2] = 'x';
  f[0][51][2] = 'y';
  f[0][85][2] = 'z';
  for (i = 0; i <= 19; i++) {
    for (j1 = 0; j1 < 256; j1++)
      for (k1 = 0; k1 < 3; k1++) f[i + 1][j1][k1] = f[i][j1][k1];
    for (j1 = 0; j1 < 256; j1++)
      for (k1 = 0; k1 < 3; k1++) {
        if (f[i][j1][k1].empty()) continue;
        if (f[i + 1][j1][2].empty()) f[i + 1][j1][2] = '(' + f[i][j1][k1] + ')';
        if (f[i + 1][j1][2].size() > f[i][j1][k1].size() + 2)
          f[i + 1][j1][2] = '(' + f[i][j1][k1] + ')';
        if (f[i + 1][j1][2].size() == f[i][j1][k1].size() + 2 &&
            f[i + 1][j1][2] > '(' + f[i][j1][k1] + ')')
          f[i + 1][j1][2] = '(' + f[i][j1][k1] + ')';
        if (k1 != 2) continue;
        if (f[i + 1][255 ^ j1][2].empty())
          f[i + 1][255 ^ j1][2] = '!' + f[i][j1][2];
        if (f[i + 1][255 ^ j1][2].size() > f[i][j1][2].size() + 1)
          f[i + 1][255 ^ j1][2] = '!' + f[i][j1][2];
        if (f[i + 1][255 ^ j1][2].size() == f[i][j1][2].size() + 1 &&
            f[i + 1][255 ^ j1][2] > '!' + f[i][j1][2])
          f[i + 1][255 ^ j1][2] = '!' + f[i][j1][2];
      }
    for (j1 = 0; j1 < 256; j1++)
      for (k1 = 0; k1 < 3; k1++)
        for (j2 = 0; j2 < 256; j2++)
          for (k2 = 0; k2 < 3; k2++) {
            if (f[i][j1][k1].empty()) continue;
            if (f[i][j2][k2].empty()) continue;
            if (f[i + 1][j1 | j2][0].empty())
              f[i + 1][j1 | j2][0] = f[i][j1][k1] + '|' + f[i][j2][k2];
            if (f[i + 1][j1 | j2][0].size() >
                f[i][j1][k1].size() + 1 + f[i][j2][k2].size())
              f[i + 1][j1 | j2][0] = f[i][j1][k1] + '|' + f[i][j2][k2];
            if (f[i + 1][j1 | j2][0].size() ==
                    f[i][j1][k1].size() + 1 + f[i][j2][k2].size() &&
                f[i + 1][j1 | j2][0] > f[i][j1][k1] + '|' + f[i][j2][k2])
              f[i + 1][j1 | j2][0] = f[i][j1][k1] + '|' + f[i][j2][k2];
            if (k1 == 0 || k2 == 0) continue;
            if (f[i + 1][j1 & j2][1].empty())
              f[i + 1][j1 & j2][1] = f[i][j1][k1] + '&' + f[i][j2][k2];
            if (f[i + 1][j1 & j2][1].size() >
                f[i][j1][k1].size() + 1 + f[i][j2][k2].size())
              f[i + 1][j1 & j2][1] = f[i][j1][k1] + '&' + f[i][j2][k2];
            if (f[i + 1][j1 & j2][1].size() ==
                    f[i][j1][k1].size() + 1 + f[i][j2][k2].size() &&
                f[i + 1][j1 & j2][1] > f[i][j1][k1] + '&' + f[i][j2][k2])
              f[i + 1][j1 & j2][1] = f[i][j1][k1] + '&' + f[i][j2][k2];
          }
  }
  scanf("%d", &n);
  int v;
  while (n--) {
    scanf("%s", st);
    v = 0;
    for (i = 0; i < 8; i++)
      if (st[i] == '1') v = v + (1 << (7 - i));
    ans = "";
    for (i = 0; i < 3; i++) {
      if (f[20][v][i].empty()) continue;
      if (ans.empty()) {
        ans = f[20][v][i];
        continue;
      }
      if (f[20][v][i].size() < ans.size()) {
        ans = f[20][v][i];
        continue;
      }
      if (f[20][v][i].size() == ans.size() && f[20][v][i] < ans) {
        ans = f[20][v][i];
        continue;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
