#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main() {
  int m, n;
  cin >> m >> n;
  unordered_map<string, vector<bool>> table0;
  unordered_map<string, vector<bool>> table1;
  table0["?"] = vector<bool>(n, false);
  table1["?"] = vector<bool>(n, true);
  auto convert = [=](string str) {
    vector<bool> tmp(n);
    for (int i = 0; i < n; ++i) {
      tmp[i] = str[i] == '1';
    }
    return tmp;
  };
  unordered_map<string, function<bool(bool, bool)>> ops;
  ops["XOR"] = std::not_equal_to<bool>();
  ops["AND"] = std::logical_and<bool>();
  ops["OR"] = std::logical_or<bool>();
  auto calc = [&](string op, vector<bool> a, vector<bool> b) {
    vector<bool> tmp(n);
    transform(a.begin(), a.end(), b.begin(), tmp.begin(), ops[op]);
    return tmp;
  };
  while (m-- > 0) {
    string left;
    string trash;
    cin >> left >> trash;
    string first;
    cin >> first;
    if (cin.peek() == '\n') {
      table0[left] = convert(first);
      table1[left] = convert(first);
    } else {
      string op, second;
      cin >> op >> second;
      table0[left] = calc(op, table0[first], table0[second]);
      table1[left] = calc(op, table1[first], table1[second]);
    }
  }
  vector<int> final0(n, 0), final1(n, 0);
  table1["?"] = vector<bool>(n, false);
  auto sumer = [](int a, bool s) { return a + (s ? 1 : 0); };
  for (auto kv : table0) {
    transform(final0.begin(), final0.end(), kv.second.begin(), final0.begin(),
              sumer);
  }
  for (auto kv : table1) {
    transform(final1.begin(), final1.end(), kv.second.begin(), final1.begin(),
              sumer);
  }
  for (int i = 0; i < n; ++i) {
    if (final0[i] == final1[i]) {
      cout << "0";
    } else {
      cout << (final0[i] < final1[i] ? '0' : '1');
    }
  }
  cout << endl;
  for (int i = 0; i < n; ++i) {
    if (final0[i] == final1[i]) {
      cout << "0";
    } else {
      cout << (final0[i] > final1[i] ? '0' : '1');
    }
  }
  cout << endl;
  return 0;
}
