#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535897932384626433832795l;
template <typename T>
inline auto sqr(T x) -> decltype(x * x) {
  return x * x;
}
template <typename T1, typename T2>
inline bool umx(T1& a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool umn(T1& a, T2 b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int N = 8;
string d[3][1 << N];
int msk[3];
bool use[3][1 << N];
bool cmp(string a, string b) {
  if (a == "~") return false;
  if (b == "~") return true;
  if (a.size() > b.size()) return false;
  if (a.size() < b.size()) return true;
  return a < b;
}
void upd(string& a, string b) {
  if (cmp(b, a)) {
    a = b;
  }
}
int main() {
  cout.setf(ios::showpoint | ios::fixed);
  cout.precision(20);
  for (int i = int(0); i < int(3); ++i)
    for (int j = int(0); j < int(1 << N); ++j) d[i][j] = "~";
  for (int j = int(0); j < int(N); ++j) {
    if (j % 2 == 1) {
      msk[2] |= (1 << j);
    }
    if ((j / 2) % 2 == 1) {
      msk[1] |= (1 << j);
    }
    if ((j / 4) % 2 == 1) {
      msk[0] |= (1 << j);
    }
  }
  d[2][msk[0]] = "x";
  d[2][msk[1]] = "y";
  d[2][msk[2]] = "z";
  memset(use, 0, sizeof(use));
  while (1) {
    string bst = "~";
    pair<int, int> bsti = make_pair(-1, -1);
    for (int i = int(0); i < int(3); ++i) {
      for (int j = int(0); j < int(1 << N); ++j) {
        if (!use[i][j] && cmp(d[i][j], bst)) {
          bsti = make_pair(i, j);
          bst = d[i][j];
        }
      }
    }
    if (bsti.first < 0) {
      break;
    }
    int i0 = bsti.first, j0 = bsti.second;
    use[i0][j0] = 1;
    for (int i1 = int(0); i1 < int(3); ++i1) {
      for (int j1 = int(0); j1 < int(1 << N); ++j1) {
        if (d[i1][j1] == "~") continue;
        upd(d[0][j0 | j1], d[i0][j0] + "|" + d[i1][j1]);
        upd(d[0][j0 | j1], d[i1][j1] + "|" + d[i0][j0]);
        if (i0 > 0 && i1 > 0) {
          upd(d[1][j0 & j1], d[i0][j0] + "&" + d[i1][j1]);
          upd(d[1][j0 & j1], d[i1][j1] + "&" + d[i0][j0]);
        }
      }
    }
    if (i0 != 2) {
      upd(d[2][j0], "(" + d[i0][j0] + ")");
    }
    if (i0 == 2) upd(d[i0][255 - j0], "!" + d[i0][j0]);
  }
  int n;
  cin >> n;
  string qwe;
  getline(cin, qwe);
  for (int i = int(0); i < int(n); ++i) {
    string s;
    getline(cin, s);
    int msk = 0;
    for (int j = int(0); j < int(((int)(s).size())); ++j) {
      if (s[j] == '1') {
        msk |= (1 << j);
      }
    }
    string res = "~";
    for (int j = int(0); j < int(3); ++j) {
      upd(res, d[j][msk]);
    }
    cout << res << endl;
  }
  return 0;
}
