#include <bits/stdc++.h>
using namespace std;
string S;
int idx;
int mp[256];
map<int, string> Emin, Tmin, Fmin;
bool E();
bool F() {
  if (S[idx] == '!') {
    ++idx;
    return (!F());
  }
  if (S[idx] == '1') {
    ++idx;
    return (1);
  }
  if (S[idx] == '0') {
    ++idx;
    return (0);
  }
  if (isalpha(S[idx])) {
    ++idx;
    return (mp[S[idx - 1]]);
  }
  ++idx;
  bool get = E();
  ++idx;
  return (get);
}
bool T() {
  int get = F();
  while (idx < S.size() && S[idx] == '&') {
    ++idx;
    int get2 = T();
    get &= get2;
  }
  return (get);
}
bool E() {
  int get = T();
  while (idx < S.size() && S[idx] == '|') {
    ++idx;
    int get2 = T();
    get |= get2;
  }
  return (get);
}
int tobit(const string &s) {
  int bit = 0;
  S = s;
  for (int j = 0; j < (1 << 3); j++) {
    mp['z'] = (j >> 0) & 1;
    mp['y'] = (j >> 1) & 1;
    mp['x'] = (j >> 2) & 1;
    idx = 0;
    bit |= E() << j;
  }
  return (bit);
}
int main() {
  vector<string> E[11451], T[11451], F[11451];
  for (int i = 1; Emin.size() < 256; i++) {
    vector<string> latte1, malta1, beet1;
    if (i == 1) {
      beet1.emplace_back("x");
      beet1.emplace_back("y");
      beet1.emplace_back("z");
    }
    if (i >= 1) {
      for (auto &s : F[i - 1]) {
        beet1.emplace_back("!" + s);
      }
    }
    if (i >= 2) {
      for (auto &s : E[i - 2]) {
        beet1.emplace_back(string("(") + s + ")");
      }
    }
    if (i >= 1) {
      malta1 = beet1;
      int sz = i - 1;
      for (int k = 1; k < sz; k++) {
        int l = sz - k;
        for (auto &&s : T[k]) {
          for (auto &&t : F[l]) {
            malta1.emplace_back(s + "&" + t);
          }
        }
      }
    }
    if (i >= 1) {
      latte1 = malta1;
      int sz = i - 1;
      for (int k = 1; k < sz; k++) {
        int l = sz - k;
        for (auto &&s : E[k]) {
          for (auto &&t : T[l]) {
            latte1.emplace_back(s + "|" + t);
          }
        }
      }
    }
    sort(begin(beet1), end(beet1));
    beet1.erase(unique(begin(beet1), end(beet1)), end(beet1));
    sort(begin(latte1), end(latte1));
    latte1.erase(unique(begin(latte1), end(latte1)), end(latte1));
    sort(begin(malta1), end(malta1));
    malta1.erase(unique(begin(malta1), end(malta1)), end(malta1));
    for (auto &s : beet1) {
      int bit = tobit(s);
      if (!Fmin.count(bit)) {
        Fmin[bit] = s;
        F[i].push_back(s);
      }
    }
    for (auto &s : malta1) {
      int bit = tobit(s);
      if (!Tmin.count(bit)) {
        Tmin[bit] = s;
        T[i].push_back(s);
      }
    }
    for (auto &s : latte1) {
      int bit = tobit(s);
      if (!Emin.count(bit)) {
        Emin[bit] = s;
        E[i].push_back(s);
      }
    }
  }
  int N;
  cin >> N;
  while (N--) {
    string bit;
    cin >> bit;
    int bits = 0;
    for (int i = 0; i < 8; i++) {
      if (bit[i] == '1') bits |= 1 << i;
    }
    cout << Emin[bits] << endl;
  }
}
