#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
void check(string& s1, string s2) {
  if (s2.length() == s1.length()) {
    if (s2 < s1) s1 = s2;
    return;
  }
  if (s2.length() < s1.length()) {
    s1 = s2;
  }
}
int x[8], y[8], z[8];
string dp[256][3];
int getval(string s) {
  int val = 0;
  for (int i = 0; i < s.length(); ++i) {
    val += val;
    val += s[i] - '0';
  }
  return val;
}
map<char, int> ma;
int main() {
  ma['!'] = 0;
  ma['|'] = 2;
  ma['&'] = 1;
  for (int i = 0; i < 256; ++i) {
    dp[i][0] = "";
    for (int j = 1; j <= 10000; ++j) dp[i][0] += '$';
    for (int j = 0; j < 3; ++j) dp[i][j] = dp[i][0];
  }
  for (int i = 0; i < 8; ++i) x[i] = (i / 4), y[i] = (i / 2) % 2, z[i] = i % 2;
  string s = "";
  for (int i = 0; i < 8; ++i) {
    s += char(x[i] + '0');
  }
  dp[getval(s)][0] = "x";
  s = "";
  for (int i = 0; i < 8; ++i) {
    s += char(y[i] + '0');
  }
  dp[getval(s)][0] = "y";
  s = "";
  for (int i = 0; i < 8; ++i) {
    s += char(z[i] + '0');
  }
  dp[getval(s)][0] = "z";
  for (int i = 1; i <= 3 * 256; ++i) {
    bool ok = 0;
    for (int j = 0; j < 256; ++j) {
      for (int k = 0; k < 256; ++k) {
        for (int t1 = 0; t1 < 3; ++t1) {
          for (int t2 = 0; t2 < 3; ++t2) {
            if (dp[j][t1][0] == '$' || dp[k][t2][0] == '$') continue;
            for (int t3 = 0; t3 < 3; ++t3) {
              int ni;
              string tmp;
              if (t3 == 0) {
                ni = 255 ^ j;
              }
              if (t3 == 1) {
                ni = j & k;
              }
              if (t3 == 2) {
                ni = j | k;
              }
              if (dp[j][t1].length() + dp[k][t2].length() >=
                  dp[ni][t3].length()) {
                continue;
              }
              if (t3 == 0) {
                if (t1 == 0)
                  tmp = "!" + dp[j][t1];
                else
                  tmp = "!(" + dp[j][t1] + ")";
              }
              if (t3 == 1) {
                if (t1 > t3) {
                  tmp = "(" + dp[j][t1] + ")";
                } else
                  tmp = dp[j][t1];
                tmp += '&';
                if (t2 > t3) {
                  tmp += "(" + dp[k][t2] + ")";
                } else
                  tmp += dp[k][t2];
              }
              if (t3 == 2) {
                if (t1 > t3) {
                  tmp = "(" + dp[j][t1] + ")";
                } else
                  tmp = dp[j][t1];
                tmp += '|';
                if (t2 > t3) {
                  tmp += "(" + dp[k][t2] + ")";
                } else
                  tmp += dp[k][t2];
              }
              string old = dp[ni][t3];
              check(dp[ni][t3], tmp);
              if (old != dp[ni][t3]) ok = 1;
            }
          }
        }
      }
    }
    if (!ok) break;
  }
  int t;
  cin >> t;
  for (int Q = 1; Q <= t; ++Q) {
    string s;
    cin >> s;
    string ss = dp[getval(s)][0];
    for (int j = 0; j < 3; ++j) check(ss, dp[getval(s)][j]);
    cout << ss << endl;
  }
  return 0;
}
