#include <bits/stdc++.h>
using namespace ::std;
const int maxn = 1e5 + 500;
const int mod = 1e9 + 7;
const int inf = 1e9 + 500;
string minn[256][800];
bool vis[256][800];
vector<pair<string, pair<int, char> > > st;
string ans[256];
string minnn(string a, string b) {
  if (a.size() < b.size()) {
    return a;
  }
  if (b.size() < a.size()) {
    return b;
  }
  if (a < b) {
    return a;
  }
  return b;
}
int nott(int a) { return 255 ^ a; }
void ft(string s, int v, char c) {
  if (vis[v][c] == 0) {
    minn[v][c] = s;
    vis[v][c] = 1;
  }
  minn[v][c] = minnn(minn[v][c], s);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  minn[15]['f'] = "x";
  minn[51]['f'] = "y";
  minn[85]['f'] = "z";
  vis[15]['f'] = 1;
  vis[51]['f'] = 1;
  vis[85]['f'] = 1;
  for (int qw = 0; qw < 20; qw++) {
    for (int v = 0; v < 256; v++) {
      if (vis[v]['f']) {
        string s = minn[v]['f'];
        ft("!" + s, nott(v), 'f');
        ft(s, v, 't');
        for (int i = 0; i < 256; i++) {
          if (vis[i]['t']) {
            ft(minn[i]['t'] + "&" + s, v & i, 't');
          }
        }
      }
      if (vis[v]['e']) {
        string s = minn[v]['e'];
        ft("(" + s + ")", v, 'f');
        for (int i = 0; i < 256; i++) {
          if (vis[i]['t']) {
            ft(s + "|" + minn[i]['t'], v | i, 'e');
          }
        }
      }
      if (vis[v]['t']) {
        string s = minn[v]['t'];
        ft(s, v, 'e');
        for (int i = 0; i < 256; i++) {
          if (vis[i]['e']) {
            ft(minn[i]['e'] + "|" + s, v | i, 'e');
          }
        }
        for (int i = 0; i < 256; i++) {
          if (vis[i]['f']) {
            ft(s + "&" + minn[i]['f'], v & i, 't');
          }
        }
      }
    }
  }
  for (int v = 0; v < 256; v++) {
    ans[v] = minnn(minnn(minn[v]['f'], minn[v]['e']), minn[v]['t']);
  }
  int n;
  cin >> n;
  cout << endl;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int v = 0;
    for (int i = 0; i < 8; i++) {
      v *= 2;
      v += (s[i] - '0');
    }
    cout << ans[v] << endl;
  }
}
