#include <bits/stdc++.h>
using namespace std;
int n, m;
struct var_t {
  int id;
  string name;
  string op;
  string values[2];
  int values_i[2];
};
var_t vars[6000];
map<string, int> var_id;
int count_var_cache[6000];
bool count_var(var_t& var_d, int b_i, int b_v) {
  ;
  auto key = var_d.id;
  if (count_var_cache[key] != -1) {
    return count_var_cache[key];
  }
  bool res = 0;
  if (var_d.op == "CONST") {
    res = var_d.values[0][b_i] == '1';
  } else {
    int value_b1 = 0;
    int value_b2 = 0;
    if (var_d.values[0] == "?") {
      value_b1 = b_v;
    } else {
      value_b1 = count_var(vars[var_d.values_i[0]], b_i, b_v);
    }
    if (var_d.values[1] == "?") {
      value_b2 = b_v;
    } else {
      value_b2 = count_var(vars[var_d.values_i[1]], b_i, b_v);
    }
    if (var_d.op == "OR") {
      res = value_b1 | value_b2;
    } else if (var_d.op == "AND") {
      res = value_b1 & value_b2;
    } else if (var_d.op == "XOR") {
      res = value_b1 ^ value_b2;
    }
  }
  count_var_cache[key] = res;
  return res;
}
int count_bits(int b_i, int b_v) {
  memset(count_var_cache, -1, sizeof(count_var_cache));
  int result = 0;
  for (int(i) = (0); (i) < (int)(n); ++(i)) {
    result += (int)count_var(vars[i], b_i, b_v);
  }
  return result;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int(i) = (0); (i) < (int)(n); ++(i)) {
    var_t var_descritpion;
    string var_name, tmp, value1;
    cin >> var_name >> tmp >> value1;
    var_id[var_name] = i;
    var_descritpion.id = i;
    var_descritpion.name = var_name;
    var_descritpion.values[0] = value1;
    if (value1[0] == '0' || value1[0] == '1') {
      var_descritpion.op = "CONST";
    } else {
      string op, value2;
      cin >> op >> value2;
      if (value1 != "?") var_descritpion.values_i[0] = var_id[value1];
      var_descritpion.op = op;
      var_descritpion.values[1] = value2;
      if (value2 != "?") var_descritpion.values_i[1] = var_id[value2];
    }
    vars[i] = var_descritpion;
    ;
  };
  string min_ans(m, '0'), max_ans(m, '0');
  for (int(i) = (0); (i) < (int)(m); ++(i)) {
    ;
    int b1_0 = count_bits(i, 0);
    int b1_1 = count_bits(i, 1);
    if (b1_0 > b1_1) {
      min_ans[i] = '1';
      max_ans[i] = '0';
    } else if (b1_0 < b1_1) {
      min_ans[i] = '0';
      max_ans[i] = '1';
    } else {
      min_ans[i] = max_ans[i] = '0';
    }
  }
  cout << min_ans << endl << max_ans << endl;
  return 0;
}
