#include <bits/stdc++.h>
using namespace std;
const int MSK = 255;
string Min(string a, string b) {
  if (b == "") return a;
  if (a == "") return b;
  if (a.size() < b.size())
    return a;
  else if (a.size() > b.size())
    return b;
  else if (a < b)
    return a;
  else
    return b;
}
string s[256];
string s1[256];
int main() {
  int val_x = MSK - 15;
  s1[val_x] = s[val_x] = "x";
  int val_y = 0;
  for (int i = 0; i < 8; i++) {
    if ((i / 2) % 2 == 1) val_y += (1 << i);
  }
  s1[val_y] = s[val_y] = "y";
  int val_z = 0;
  for (int i = 0; i < 8; i++) {
    if (i % 2 == 1) val_z += (1 << i);
  }
  s1[val_z] = s[val_z] = "z";
  for (int ITR = 0; ITR < 10; ITR++) {
    for (int i = 0; i < 256; i++) {
      if (s[i] == "") continue;
      if (i == val_x || i == val_y || i == val_z) {
        s[MSK - i] = Min("!" + s[i], s[MSK - i]);
        s1[MSK - i] = Min("!" + s[i], s1[MSK - i]);
      } else {
        s[MSK - i] = Min("!(" + s[i] + ")", s[MSK - i]);
        s1[MSK - i] = Min("!(" + s[i] + ")", s1[MSK - i]);
      }
    }
    for (int i = 0; i < 256; i++) {
      if (s1[i] == "") continue;
      if (i == val_x || i == val_y || i == val_z) {
        s[MSK - i] = Min("!" + s1[i], s[MSK - i]);
        s1[MSK - i] = Min("!" + s1[i], s1[MSK - i]);
      } else {
        s[MSK - i] = Min("!(" + s1[i] + ")", s[MSK - i]);
        s1[MSK - i] = Min("!(" + s1[i] + ")", s1[MSK - i]);
      }
    }
    for (int i = 0; i < 256; i++) {
      if (s[i] == "") continue;
      for (int j = 0; j < 256; j++) {
        if (s[j] == "") continue;
        s[i & j] = Min("(" + s[i] + ")&(" + s[j] + ")", s[i & j]);
        s1[i & j] = Min("(" + s[i] + ")&(" + s[j] + ")", s1[i & j]);
        s[i | j] = Min(s[i] + "|" + s[j], s[i | j]);
        s1[i | j] = Min("(" + s[i] + "|" + s[j] + ")", s1[i | j]);
        bool i_ok = true;
        int cnt = 0;
        for (int t = 0; t < s[i].size(); t++) {
          if (s[i][t] == '(')
            cnt++;
          else if (s[i][t] == ')')
            cnt--;
          else if (s[i][t] == '|' && cnt == 0)
            i_ok = false;
        }
        bool j_ok = true;
        cnt = 0;
        for (int t = 0; t < s[j].size(); t++) {
          if (s[j][t] == '(')
            cnt++;
          else if (s[j][t] == ')')
            cnt--;
          else if (s[j][t] == '|' && cnt == 0)
            j_ok = false;
        }
        if (i_ok) {
          s[i & j] = Min(s[i] + "&(" + s[j] + ")", s[i & j]);
          s1[i & j] = Min(s[i] + "&(" + s[j] + ")", s1[i & j]);
        }
        if (j_ok) {
          s[i & j] = Min("(" + s[i] + ")&" + s[j], s[i & j]);
          s1[i & j] = Min("(" + s[i] + ")&" + s[j], s1[i & j]);
        }
        if (i_ok && j_ok) {
          s[i & j] = Min(s[i] + "&" + s[j], s[i & j]);
          s1[i & j] = Min(s[i] + "&" + s[j], s1[i & j]);
        }
      }
    }
    for (int i = 0; i < 256; i++) {
      if (s1[i] == "") continue;
      for (int j = 0; j < 256; j++) {
        if (s1[j] == "") continue;
        s1[i & j] = Min(s1[i] + "&" + s1[j], s1[i & j]);
        s1[i | j] = Min("(" + s1[i] + "|" + s1[j] + ")", s1[i | j]);
        s[i & j] = Min(s1[i] + "&" + s1[j], s[i & j]);
      }
    }
    for (int i = 0; i < 256; i++) {
      if (s1[i] == "") continue;
      for (int j = 0; j < 256; j++) {
        if (s[j] == "") continue;
        s[i & j] = Min(s1[i] + "&(" + s[j] + ")", s[i & j]);
        s1[i & j] = Min(s1[i] + "&(" + s[j] + ")", s1[i & j]);
      }
    }
  }
  int n;
  cin >> n;
  while (n--) {
    string in;
    cin >> in;
    int val = 0;
    for (int i = 7; i >= 0; i--) {
      val = (in[i] - '0') + val * 2;
    }
    cout << s[val] << endl;
  }
  return 0;
}
