#include <bits/stdc++.h>
using namespace std;
struct fake_ostream {
} fout;
template <class T>
const fake_ostream &operator<<(const fake_ostream &out, const T &val) {
  return out;
}
string ans[256][3];
void minimize(string &s, const string &s2) {
  if (s.length() && s.length() < s2.length()) return;
  if (s.length() == s2.length()) {
    s = min(s, s2);
    return;
  }
  s = s2;
}
template <class input, class output>
void solution(input &cin, output &cout) {
  const int x = 15, y = 3 * 17, z = 5 * 17;
  ans[x][0] = "x";
  ans[y][0] = "y";
  ans[z][0] = "z";
  for (int len = 1; len <= 72; len++) {
    for (int mask = 0; mask < 256; mask++) {
      if (ans[mask][0].length()) minimize(ans[mask][1], ans[mask][0]);
      if (ans[mask][1].length()) minimize(ans[mask][2], ans[mask][1]);
      if (ans[mask][2].length())
        if (ans[mask][0].length() == 0 ||
            ans[mask][0].length() >= ans[mask][2].length() + 2)
          minimize(ans[mask][0], "(" + ans[mask][2] + ")");
    }
    for (int mask = 0; mask < 256; mask++) {
      if (ans[mask][0].length()) {
        if (ans[(255 & (~(mask)))][0].length() == 0 ||
            ans[(255 & (~(mask)))][0].length() >= ans[mask][0].length() + 1)
          minimize(ans[(255 & (~(mask)))][0], "!" + ans[mask][0]);
      }
      if (ans[mask][1].length()) {
        for (int mask2 = 0; mask2 < 256; mask2++)
          if (ans[mask2][1].length())
            if (ans[mask & mask2][1].length() == 0 ||
                ans[mask & mask2][1].length() >=
                    ans[mask][1].length() + ans[mask2][1].length() + 1)
              minimize(ans[mask & mask2][1],
                       ans[mask][1] + "&" + ans[mask2][1]);
      }
      if (ans[mask][2].length()) {
        for (int mask2 = 0; mask2 < 256; mask2++)
          if (ans[mask2][2].length())
            if (ans[mask | mask2][2].length() == 0 ||
                ans[mask | mask2][2].length() >=
                    ans[mask][2].length() + ans[mask2][2].length() + 1)
              minimize(ans[mask | mask2][2],
                       ans[mask][2] + "|" + ans[mask2][2]);
      }
    }
  }
  fout << ans[(y | z) & x][2] << "\n";
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int kek = 0;
    for (char c : s) {
      kek *= 2;
      if (c == '1') kek++;
    }
    cout << ans[kek][2] << "\n";
  }
}
int32_t main() {
  solution(cin, cout);
  return 0;
}
