#include <bits/stdc++.h>
enum ops { NONE, AND, OR, XOR };
struct Variable {
  bool isExpr;
  std::string value;
  std::string lArg;
  ops op;
  std::string rArg;
  Variable() {}
  Variable(bool isExpr, const std::string &value, const std::string &lArg,
           ops op, const std::string &rArg)
      : isExpr(isExpr), value(value), lArg(lArg), op(op), rArg(rArg) {}
};
int computeSum(const std::vector<std::string> &names,
               std::unordered_map<std::string, Variable> &vars, int pos);
int main() {
  std::ios_base::sync_with_stdio(false);
  int n, m;
  std::cin >> n >> m;
  std::vector<std::string> names(n);
  std::unordered_map<std::string, Variable> vars;
  for (int i = 0; i < n; ++i) {
    std::string eq, value, lArg, opStr, rArg;
    std::cin >> names[i] >> eq >> value;
    bool isExpr = (value[0] != '0' and value[0] != '1');
    if (isExpr) {
      lArg = value;
      value = std::string(m, '0');
      std::cin >> opStr >> rArg;
    }
    ops op = ops((opStr == "AND") + 2 * (opStr == "OR") + 3 * (opStr == "XOR"));
    vars[names[i]] = Variable(isExpr, value, lArg, op, rArg);
  }
  vars["?"] = Variable(false, std::string(m, '0'), "", NONE, "");
  std::string minMin(m, '0');
  std::string minMax(m, '0');
  for (int pos = 0; pos < m; ++pos) {
    vars["?"].value[pos] = '0';
    int sum0 = computeSum(names, vars, pos);
    vars["?"].value[pos] = '1';
    int sum1 = computeSum(names, vars, pos);
    minMin[pos] = (sum0 <= sum1 ? '0' : '1');
    minMax[pos] = (sum0 >= sum1 ? '0' : '1');
  }
  std::cout << minMin << std::endl;
  std::cout << minMax << std::endl;
  return 0;
}
int computeSum(const std::vector<std::string> &names,
               std::unordered_map<std::string, Variable> &vars, int pos) {
  int sum = 0;
  for (const auto &name : names) {
    Variable &var = vars.at(name);
    if (var.isExpr) {
      int lBit = (vars.at(var.lArg).value[pos] == '1');
      int rBit = (vars.at(var.rArg).value[pos] == '1');
      int res = 0;
      switch (var.op) {
        case AND:
          res = lBit & rBit;
          break;
        case OR:
          res = lBit | rBit;
          break;
        case XOR:
          res = lBit ^ rBit;
          break;
      }
      sum += res;
      var.value[pos] = (res ? '1' : '0');
    }
  }
  return sum;
}
