#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 5;
struct Variable {
  string value;
  string operand1;
  string operand2;
  string op;
};
unordered_map<string, int> var_map;
Variable vars[N];
int m;
int n;
bool GetValue(const string& s, int bit_ind, int bit_val, short* values) {
  bool val;
  if (s == "?") {
    val = bit_val;
  } else {
    val = values[var_map[s]];
  }
  return val;
}
bool GetRes(const string& op, bool a, bool b) {
  if (op == "AND") return a & b;
  if (op == "OR") return a | b;
  if (op == "XOR") return a ^ b;
}
void Solve() {
  string res_min(m, '0');
  string res_max(m, '0');
  for (int bit_ind = 0; bit_ind < m; ++bit_ind) {
    int best_min = N;
    int best_max = 0;
    for (int bit_val = 0; bit_val < 2; ++bit_val) {
      int cnt = 0;
      short values[N];
      for (int var_ind = 0; var_ind < n; ++var_ind) {
        const auto& var = vars[var_ind];
        if (!var.value.empty()) {
          values[var_ind] = var.value[bit_ind] - '0';
          continue;
        }
        bool op1_val = GetValue(var.operand1, bit_ind, bit_val, values);
        bool op2_val = GetValue(var.operand2, bit_ind, bit_val, values);
        bool val = GetRes(var.op, op1_val, op2_val);
        values[var_ind] = val;
        cnt += val;
      }
      if (cnt < best_min) {
        best_min = cnt;
        res_min[bit_ind] = bit_val + '0';
      }
      if (cnt > best_max) {
        best_max = cnt;
        res_max[bit_ind] = bit_val + '0';
      }
    }
  }
  cout << res_min << '\n' << res_max << '\n';
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    string var_name;
    cin >> var_name;
    var_map[var_name] = i;
    string tmp;
    cin >> tmp;
    string value;
    cin >> value;
    if (isdigit(value[0])) {
      vars[i].value = value;
    } else {
      Variable var;
      var.operand1 = value;
      cin >> var.op >> var.operand2;
      vars[i] = var;
    }
  }
  Solve();
}
