#include <bits/stdc++.h>
using namespace std;
const int N = 256;
string dp[N][5];
string ans[N];
int main() {
  srand(time(NULL));
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string inf;
  for (int i = 0; i < 100; i++) inf += 'q';
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 5; j++) dp[i][j] = inf;
  }
  dp[240][1] = "x";
  dp[204][1] = "y";
  dp[170][1] = "z";
  for (int o = 0; o < 100; o++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 5; j++) {
        if (dp[i][0].size() < dp[i][j].size() + 2) continue;
        if (dp[i][j].size() + 2 < dp[i][0].size() ||
            "(" + dp[i][j] + ")" < dp[i][0])
          dp[i][0] = "(" + dp[i][j] + ")";
      }
      for (int j = 0; j < 3; j++) {
        if (dp[255 - i][2].size() < dp[i][j].size() + 1) continue;
        if (dp[i][j].size() + 1 < dp[255 - i][2].size() ||
            "!" + dp[i][j] < dp[255 - i][2])
          dp[255 - i][2] = "!" + dp[i][j];
      }
      for (int j = 0; j < 5; j++) {
        if (dp[i][j].size() < 100) {
          for (int k = 0; k < N; k++) {
            for (int l = 0; l < 5; l++) {
              if (j != 4 && l != 4) {
                if (dp[i & k][3].size() >
                        dp[i][j].size() + dp[k][l].size() + 1 ||
                    (dp[i & k][3].size() ==
                         dp[i][j].size() + dp[k][l].size() + 1 &&
                     dp[i & k][3] > dp[i][j] + "&" + dp[k][l])) {
                  dp[i & k][3] = dp[i][j] + "&" + dp[k][l];
                }
              }
              if (dp[i | k][4].size() > dp[i][j].size() + dp[k][l].size() + 1 ||
                  (dp[i | k][4].size() ==
                       dp[i][j].size() + dp[k][l].size() + 1 &&
                   dp[i | k][4] > dp[i][j] + "|" + dp[k][l])) {
                dp[i | k][4] = dp[i][j] + "|" + dp[k][l];
              }
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < N; i++) {
    ans[i] = inf;
    for (int j = 0; j < 5; j++) {
      if (dp[i][j].size() < ans[i].size() ||
          (dp[i][j].size() == ans[i].size() && dp[i][j] < ans[i])) {
        ans[i] = dp[i][j];
      }
    }
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string t;
    cin >> t;
    int msk = 0;
    for (int j = 0; j < 8; j++)
      if (t[j] == '1') msk += (1 << j);
    cout << ans[msk] << "\n";
  }
  return 0;
}
