#include <bits/stdc++.h>
using namespace std;
const string INF = "~";
int n, i, j;
string dp[3][500];
string min(string a, string b) {
  if (a == INF) return b;
  if (b == INF) return a;
  if (a.size() < b.size()) return a;
  if (a.size() > b.size()) return b;
  if (a < b) return a;
  return b;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  for (i = 0; i < 3; i++) fill(dp[i], dp[i] + 257, INF);
  dp[0][2 + 8 + 32 + 128] = "z";
  dp[0][4 + 8 + 64 + 128] = "y";
  dp[0][16 + 32 + 64 + 128] = "x";
  int t = clock(), p = 1 << 8;
  while (clock() - t < 2500) {
    for (i = 0; i < p; i++) {
      for (j = 0; j < p; j++) {
        if (dp[1][i] != INF && dp[0][j] != INF) {
          dp[1][i & j] = min(dp[1][i & j], dp[1][i] + "&" + dp[0][j]);
        }
      }
      if (dp[0][i] != INF) dp[1][i] = min(dp[1][i], dp[0][i]);
    }
    for (i = 0; i < p; i++) {
      for (j = 0; j < p; j++) {
        if (dp[2][i] != INF && dp[1][j] != INF) {
          dp[2][i | j] = min(dp[2][i | j], dp[2][i] + "|" + dp[1][j]);
        }
      }
      if (dp[1][i] != INF) dp[2][i] = min(dp[2][i], dp[1][i]);
    }
    for (i = 0; i < p; i++) {
      if (dp[2][i] != INF) dp[0][i] = min(dp[0][i], "(" + dp[2][i] + ")");
      if (dp[0][i] != INF)
        dp[0][i ^ (p - 1)] = min(dp[0][i ^ (p - 1)], "!" + dp[0][i]);
    }
  }
  cin >> n;
  for (i = 0; i < n; i++) {
    string s;
    int mask = 0;
    cin >> s;
    for (j = 0; j < 8; j++) mask |= (s[j] - '0') << j;
    cout << dp[2][mask] << "\n";
  }
  return 0;
}
