#include <bits/stdc++.h>
int sum(const std::vector<std::vector<std::string> >& commands, int bit_pos,
        int bit_value) {
  std::unordered_map<std::string, int> variables;
  variables["?"] = bit_value;
  for (int i = 0; i < commands.size(); ++i) {
    if (commands[i].size() == 2) {
      variables[commands[i][0]] = commands[i][1][bit_pos] - '0';
    } else {
      if (commands[i][2] == "AND") {
        variables[commands[i][0]] =
            variables[commands[i][1]] & variables[commands[i][3]];
      } else if (commands[i][2] == "OR") {
        variables[commands[i][0]] =
            variables[commands[i][1]] | variables[commands[i][3]];
      } else {
        variables[commands[i][0]] =
            variables[commands[i][1]] ^ variables[commands[i][3]];
      }
    }
  }
  int counter = 0;
  for (auto it = variables.begin(); it != variables.end(); ++it)
    counter += it->second;
  return counter - bit_value;
}
std::vector<std::vector<std::string> > parse(
    const std::vector<std::string>& commands) {
  std::vector<std::vector<std::string> > result;
  for (int i = 0; i < commands.size(); ++i) {
    std::vector<std::string> lines;
    int pos = (int)commands[i].find(' ');
    std::string variable_name = commands[i].substr(0, pos);
    lines.push_back(variable_name);
    std::string expression = commands[i].substr(pos + 4);
    if (expression[0] == '0' || expression[0] == '1') {
      lines.push_back(expression);
    } else {
      int pos1 = (int)expression.find(' ');
      std::string lhs = expression.substr(0, pos1);
      int pos2 = (int)expression.find(' ', pos1 + 1);
      std::string operation = expression.substr(pos1 + 1, pos2 - pos1 - 1);
      std::string rhs = expression.substr(pos2 + 1);
      lines.push_back(lhs);
      lines.push_back(operation);
      lines.push_back(rhs);
    }
    result.push_back(lines);
  }
  return result;
}
int main() {
  int num_commands, bits;
  std::cin >> num_commands >> bits;
  std::vector<std::string> commands(num_commands);
  std::string temp;
  std::getline(std::cin, temp);
  for (int i = 0; i < num_commands; ++i) {
    std::getline(std::cin, commands[i]);
  }
  std::string small;
  std::string big;
  auto parsed = parse(commands);
  for (int bit = 0; bit < bits; ++bit) {
    int a = sum(parsed, bit, 0);
    int b = sum(parsed, bit, 1);
    if (a == b) {
      small.push_back('0');
      big.push_back('0');
    } else if (a < b) {
      small.push_back('0');
      big.push_back('1');
    } else {
      small.push_back('1');
      big.push_back('0');
    }
  }
  std::cout << small << std::endl << big;
}
