#include <bits/stdc++.h>
using namespace std;
inline int getInt() {
  int res(0);
  char c = getchar();
  while (c < '0') c = getchar();
  while (c >= '0') {
    res = res * 10 + (c - '0');
    c = getchar();
  }
  return res;
}
inline int fastpo(int x, int n, int mod) {
  int res(1);
  while (n) {
    if (n & 1) {
      res = res * (long long)x % mod;
    }
    x = x * (long long)x % mod;
    n /= 2;
  }
  return res;
}
const int N = 300033;
const int LOG = 20;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
string tab[256][2] = {
    {"00000000", "!x&x"},
    {"00000001", "!(x|y|z)"},
    {"00000010", "!x&!y&z"},
    {"00000011", "!x&!y"},
    {"00000100", "!x&!z&y"},
    {"00000101", "!x&!z"},
    {"00000110", "!(!y&!z|x|y&z)"},
    {"00000111", "!(x|y&z)"},
    {"00001000", "!x&y&z"},
    {"00001001", "!(!y&z|!z&y|x)"},
    {"00001010", "!x&z"},
    {"00001011", "!(!z&y|x)"},
    {"00001100", "!x&y"},
    {"00001101", "!(!y&z|x)"},
    {"00001110", "!x&(y|z)"},
    {"00001111", "!x"},
    {"00010000", "!y&!z&x"},
    {"00010001", "!y&!z"},
    {"00010010", "!(!x&!z|x&z|y)"},
    {"00010011", "!(x&z|y)"},
    {"00010100", "!(!x&!y|x&y|z)"},
    {"00010101", "!(x&y|z)"},
    {"00010110", "!(!x&!y|x&y|z)|!x&!y&z"},
    {"00010111", "!((x|y)&z|x&y)"},
    {"00011000", "!x&y&z|!y&!z&x"},
    {"00011001", "!x&y&z|!y&!z"},
    {"00011010", "!x&z|!y&!z&x"},
    {"00011011", "!x&z|!y&!z"},
    {"00011100", "!x&y|!y&!z&x"},
    {"00011101", "!x&y|!y&!z"},
    {"00011110", "!x&(y|z)|!y&!z&x"},
    {"00011111", "!x|!y&!z"},
    {"00100000", "!y&x&z"},
    {"00100001", "!(!x&z|!z&x|y)"},
    {"00100010", "!y&z"},
    {"00100011", "!(!z&x|y)"},
    {"00100100", "!x&!z&y|!y&x&z"},
    {"00100101", "!x&!z|!y&x&z"},
    {"00100110", "!x&!z&y|!y&z"},
    {"00100111", "!x&!z|!y&z"},
    {"00101000", "!x&y&z|!y&x&z"},
    {"00101001", "!(!x&z|!z&x|y)|!x&y&z"},
    {"00101010", "!(x&y)&z"},
    {"00101011", "!(!z&x|y)|!x&z"},
    {"00101100", "!x&y|!y&x&z"},
    {"00101101", "!(!y&z|x)|!y&x&z"},
    {"00101110", "!x&y|!y&z"},
    {"00101111", "!x|!y&z"},
    {"00110000", "!y&x"},
    {"00110001", "!(!x&z|y)"},
    {"00110010", "!y&(x|z)"},
    {"00110011", "!y"},
    {"00110100", "!x&!z&y|!y&x"},
    {"00110101", "!x&!z|!y&x"},
    {"00110110", "!x&!z&y|!y&(x|z)"},
    {"00110111", "!x&!z|!y"},
    {"00111000", "!x&y&z|!y&x"},
    {"00111001", "!(!x&z|y)|!x&y&z"},
    {"00111010", "!x&z|!y&x"},
    {"00111011", "!x&z|!y"},
    {"00111100", "!x&y|!y&x"},
    {"00111101", "!(!x&!y&z|x&y)"},
    {"00111110", "!x&(y|z)|!y&x"},
    {"00111111", "!x|!y"},
    {"01000000", "!z&x&y"},
    {"01000001", "!(!x&y|!y&x|z)"},
    {"01000010", "!x&!y&z|!z&x&y"},
    {"01000011", "!x&!y|!z&x&y"},
    {"01000100", "!z&y"},
    {"01000101", "!(!y&x|z)"},
    {"01000110", "!x&!y&z|!z&y"},
    {"01000111", "!x&!y|!z&y"},
    {"01001000", "!x&y&z|!z&x&y"},
    {"01001001", "!(!x&y|!y&x|z)|!x&y&z"},
    {"01001010", "!x&z|!z&x&y"},
    {"01001011", "!(!z&y|x)|!z&x&y"},
    {"01001100", "!(x&z)&y"},
    {"01001101", "!(!y&x|z)|!x&y"},
    {"01001110", "!x&z|!z&y"},
    {"01001111", "!x|!z&y"},
    {"01010000", "!z&x"},
    {"01010001", "!(!x&y|z)"},
    {"01010010", "!x&!y&z|!z&x"},
    {"01010011", "!x&!y|!z&x"},
    {"01010100", "!z&(x|y)"},
    {"01010101", "!z"},
    {"01010110", "!x&!y&z|!z&(x|y)"},
    {"01010111", "!x&!y|!z"},
    {"01011000", "!x&y&z|!z&x"},
    {"01011001", "!(!x&y|z)|!x&y&z"},
    {"01011010", "!x&z|!z&x"},
    {"01011011", "!(!x&!z&y|x&z)"},
    {"01011100", "!x&y|!z&x"},
    {"01011101", "!x&y|!z"},
    {"01011110", "!x&(y|z)|!z&x"},
    {"01011111", "!x|!z"},
    {"01100000", "!y&x&z|!z&x&y"},
    {"01100001", "!(!x&y|!y&x|z)|!y&x&z"},
    {"01100010", "!y&z|!z&x&y"},
    {"01100011", "!(!z&x|y)|!z&x&y"},
    {"01100100", "!y&x&z|!z&y"},
    {"01100101", "!(!y&x|z)|!y&x&z"},
    {"01100110", "!y&z|!z&y"},
    {"01100111", "!(!y&!z&x|y&z)"},
    {"01101000", "!x&y&z|!y&x&z|!z&x&y"},
    {"01101001", "!(!x&y|!y&x|z)|!x&y&z|!y&x&z"},
    {"01101010", "!(x&y)&z|!z&x&y"},
    {"01101011", "!(!z&x|y)|!x&z|!z&x&y"},
    {"01101100", "!(x&z)&y|!y&x&z"},
    {"01101101", "!(!y&x|z)|!x&y|!y&x&z"},
    {"01101110", "!(x&y)&z|!z&y"},
    {"01101111", "!x|!y&z|!z&y"},
    {"01110000", "!(y&z)&x"},
    {"01110001", "!(!x&y|z)|!y&x"},
    {"01110010", "!y&z|!z&x"},
    {"01110011", "!y|!z&x"},
    {"01110100", "!y&x|!z&y"},
    {"01110101", "!y&x|!z"},
    {"01110110", "!y&(x|z)|!z&y"},
    {"01110111", "!y|!z"},
    {"01111000", "!(y&z)&x|!x&y&z"},
    {"01111001", "!(!x&y|z)|!x&y&z|!y&x"},
    {"01111010", "!(x&y)&z|!z&x"},
    {"01111011", "!x&z|!y|!z&x"},
    {"01111100", "!(x&z)&y|!y&x"},
    {"01111101", "!x&y|!y&x|!z"},
    {"01111110", "!x&y|!y&z|!z&x"},
    {"01111111", "!(x&y&z)"},
    {"10000000", "x&y&z"},
    {"10000001", "!(x|y|z)|x&y&z"},
    {"10000010", "!x&!y&z|x&y&z"},
    {"10000011", "!x&!y|x&y&z"},
    {"10000100", "!x&!z&y|x&y&z"},
    {"10000101", "!x&!z|x&y&z"},
    {"10000110", "!(!y&!z|x|y&z)|x&y&z"},
    {"10000111", "!(x|y&z)|x&y&z"},
    {"10001000", "y&z"},
    {"10001001", "!(x|y|z)|y&z"},
    {"10001010", "!x&z|y&z"},
    {"10001011", "!x&!y|y&z"},
    {"10001100", "!x&y|y&z"},
    {"10001101", "!x&!z|y&z"},
    {"10001110", "!x&(y|z)|y&z"},
    {"10001111", "!x|y&z"},
    {"10010000", "!y&!z&x|x&y&z"},
    {"10010001", "!y&!z|x&y&z"},
    {"10010010", "!(!x&!z|x&z|y)|x&y&z"},
    {"10010011", "!(x&z|y)|x&y&z"},
    {"10010100", "!(!x&!y|x&y|z)|x&y&z"},
    {"10010101", "!(x&y|z)|x&y&z"},
    {"10010110", "!(!x&!y|x&y|z)|!x&!y&z|x&y&z"},
    {"10010111", "!((x|y)&z|x&y)|x&y&z"},
    {"10011000", "!y&!z&x|y&z"},
    {"10011001", "!y&!z|y&z"},
    {"10011010", "!x&z|!y&!z&x|y&z"},
    {"10011011", "!(x&z|y)|y&z"},
    {"10011100", "!x&y|!y&!z&x|y&z"},
    {"10011101", "!(x&y|z)|y&z"},
    {"10011110", "!x&(y|z)|!y&!z&x|y&z"},
    {"10011111", "!x|!y&!z|y&z"},
    {"10100000", "x&z"},
    {"10100001", "!(x|y|z)|x&z"},
    {"10100010", "!y&z|x&z"},
    {"10100011", "!x&!y|x&z"},
    {"10100100", "!x&!z&y|x&z"},
    {"10100101", "!x&!z|x&z"},
    {"10100110", "!x&!z&y|!y&z|x&z"},
    {"10100111", "!(x|y&z)|x&z"},
    {"10101000", "(x|y)&z"},
    {"10101001", "!(x|y|z)|(x|y)&z"},
    {"10101010", "z"},
    {"10101011", "!x&!y|z"},
    {"10101100", "!x&y|x&z"},
    {"10101101", "!(!y&z|x)|x&z"},
    {"10101110", "!x&y|z"},
    {"10101111", "!x|z"},
    {"10110000", "!y&x|x&z"},
    {"10110001", "!y&!z|x&z"},
    {"10110010", "!y&(x|z)|x&z"},
    {"10110011", "!y|x&z"},
    {"10110100", "!x&!z&y|!y&x|x&z"},
    {"10110101", "!(x&y|z)|x&z"},
    {"10110110", "!x&!z&y|!y&(x|z)|x&z"},
    {"10110111", "!x&!z|!y|x&z"},
    {"10111000", "!y&x|y&z"},
    {"10111001", "!(!x&z|y)|y&z"},
    {"10111010", "!y&x|z"},
    {"10111011", "!y|z"},
    {"10111100", "!x&y|!y&x|x&z"},
    {"10111101", "!x&!z|!y&x|y&z"},
    {"10111110", "!x&y|!y&x|z"},
    {"10111111", "!x|!y|z"},
    {"11000000", "x&y"},
    {"11000001", "!(x|y|z)|x&y"},
    {"11000010", "!x&!y&z|x&y"},
    {"11000011", "!x&!y|x&y"},
    {"11000100", "!z&y|x&y"},
    {"11000101", "!x&!z|x&y"},
    {"11000110", "!x&!y&z|!z&y|x&y"},
    {"11000111", "!(x|y&z)|x&y"},
    {"11001000", "(x|z)&y"},
    {"11001001", "!(x|y|z)|(x|z)&y"},
    {"11001010", "!x&z|x&y"},
    {"11001011", "!(!z&y|x)|x&y"},
    {"11001100", "y"},
    {"11001101", "!x&!z|y"},
    {"11001110", "!x&z|y"},
    {"11001111", "!x|y"},
    {"11010000", "!z&x|x&y"},
    {"11010001", "!y&!z|x&y"},
    {"11010010", "!x&!y&z|!z&x|x&y"},
    {"11010011", "!(x&z|y)|x&y"},
    {"11010100", "!z&(x|y)|x&y"},
    {"11010101", "!z|x&y"},
    {"11010110", "!x&!y&z|!z&(x|y)|x&y"},
    {"11010111", "!x&!y|!z|x&y"},
    {"11011000", "!z&x|y&z"},
    {"11011001", "!(!x&y|z)|y&z"},
    {"11011010", "!x&z|!z&x|x&y"},
    {"11011011", "!x&!y|!z&x|y&z"},
    {"11011100", "!z&x|y"},
    {"11011101", "!z|y"},
    {"11011110", "!x&z|!z&x|y"},
    {"11011111", "!x|!z|y"},
    {"11100000", "(y|z)&x"},
    {"11100001", "!(x|y|z)|(y|z)&x"},
    {"11100010", "!y&z|x&y"},
    {"11100011", "!(!z&x|y)|x&y"},
    {"11100100", "!z&y|x&z"},
    {"11100101", "!(!y&x|z)|x&z"},
    {"11100110", "!y&z|!z&y|x&y"},
    {"11100111", "!x&!y|!z&y|x&z"},
    {"11101000", "(x|y)&z|x&y"},
    {"11101001", "!(x|y|z)|(x|y)&z|x&y"},
    {"11101010", "x&y|z"},
    {"11101011", "!x&!y|x&y|z"},
    {"11101100", "x&z|y"},
    {"11101101", "!x&!z|x&z|y"},
    {"11101110", "y|z"},
    {"11101111", "!x|y|z"},
    {"11110000", "x"},
    {"11110001", "!y&!z|x"},
    {"11110010", "!y&z|x"},
    {"11110011", "!y|x"},
    {"11110100", "!z&y|x"},
    {"11110101", "!z|x"},
    {"11110110", "!y&z|!z&y|x"},
    {"11110111", "!y|!z|x"},
    {"11111000", "x|y&z"},
    {"11111001", "!y&!z|x|y&z"},
    {"11111010", "x|z"},
    {"11111011", "!y|x|z"},
    {"11111100", "x|y"},
    {"11111101", "!z|x|y"},
    {"11111110", "x|y|z"},
    {"11111111", "!x|x"},
};
int main() {
  int n;
  cin >> n;
  map<string, string> mp;
  for (int i(0); i < 256; i++) {
    mp[tab[i][0]] = tab[i][1];
  }
  for (int i(0); i < n; i++) {
    string tmp;
    cin >> tmp;
    reverse(tmp.begin(), tmp.end());
    cout << mp[tmp] << endl;
  }
}
