#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline void priv(vector<int> a) {
  for (int i = 0; i < ((int)(a).size()); ++i)
    printf("%d%c", a[i], i == (int)(a).size() - 1 ? '\n' : ' ');
}
template <typename T>
istream& operator>>(istream& i, vector<T>& v) {
  for (int j = 0; j < ((int)(v).size()); ++j) i >> v[j];
  return i;
}
template <typename T>
string join(const vector<T>& v) {
  stringstream s;
  for (int i = 0; i < ((int)(v).size()); ++i) s << ' ' << v[i];
  return s.str().substr(1);
}
template <typename T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  if ((int)(v).size()) o << join(v);
  return o;
}
template <typename T1, typename T2>
istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& o, const pair<T1, T2>& v) {
  return o << v.first << "," << v.second;
}
const int MX = 200005, INF = 1001001001;
const long long int LINF = 1e18;
const double eps = 1e-10;
const int n = 8, n2 = 1 << n, n1 = n2 - 1;
map<int, string> me, mt, mf;
bool up(map<int, string>& mp, int x, string now) {
  if (!mp.count(x)) {
    mp[x] = now;
    return true;
  }
  if ((int)(mp[x]).size() < (int)(now).size()) return false;
  if ((int)(mp[x]).size() == (int)(now).size() && mp[x] <= now) return false;
  mp[x] = now;
  return true;
}
void init() {
  vector<int> ne, nt, nf;
  for (int i = 0; i < (3); ++i) {
    int x = 0;
    for (int j = 0; j < (8); ++j)
      if (j >> i & 1) x |= 1 << j;
    me[x] = string(1, 'z' - i);
    ne.push_back(x);
    mt[x] = string(1, 'z' - i);
    nt.push_back(x);
    mf[x] = string(1, 'z' - i);
    nf.push_back(x);
  }
  bool upd = true;
  while (upd) {
    upd = false;
    {
      for (auto q : mt) {
        int x = q.first;
        if (up(me, x, q.second)) upd = true;
        auto mp = me;
        for (auto p : mp) {
          int nx = p.first | x;
          if (up(me, nx, p.second + "|" + q.second)) upd = true;
        }
      }
    }
    {
      for (auto q : mf) {
        int x = q.first;
        if (up(mt, x, q.second)) upd = true;
        auto mp = mt;
        for (auto p : mp) {
          int nx = p.first & x;
          if (up(mt, nx, p.second + "&" + q.second)) upd = true;
        }
      }
    }
    {
      auto mp = mf;
      for (auto p : mf) {
        int nx = p.first ^ n1;
        if (up(mf, nx, "!" + p.second)) upd = true;
      }
    }
    {
      for (auto p : me) {
        if (up(mf, p.first, "(" + p.second + ")")) upd = true;
      }
    }
  }
}
int main() {
  init();
  int q;
  scanf("%d", &q);
  for (int qi = 0; qi < (q); ++qi) {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    int x = 0;
    for (int i = 0; i < (n); ++i) x = x << 1 | (s[i] - '0');
    cout << me[x] << endl;
  }
  return 0;
}
