#include <bits/stdc++.h>
using namespace std;
int N, M, t[5020];
string op[5020], operand[5020][3], var[5020];
string ans_min, ans_max;
unordered_map<string, int> mp;
unordered_map<string, bool> viz;
int oper(int x, int y, string &op) {
  if (op == "AND")
    return x & y;
  else if (op == "OR")
    return x | y;
  return x ^ y;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> M;
  ans_min.resize(M), ans_max.resize(M);
  int i;
  string aux;
  for (i = 1; i <= N; i++) {
    cin >> var[i];
    cin >> aux;
    cin >> operand[i][1];
    if (operand[i][1] == "?" || viz[operand[i][1]]) {
      cin >> op[i] >> operand[i][2];
    }
    viz[var[i]] = 1;
  }
  int cnt[2];
  int p, bit;
  for (p = 0; p < M; p++) {
    cnt[0] = cnt[1] = 0;
    for (bit = 0; bit <= 1; bit++) {
      mp.clear();
      for (i = 1; i <= N; i++) {
        if (op[i] == "")
          mp[var[i]] = operand[i][1][p] - '0';
        else {
          if (operand[i][1] == "?" && operand[i][2] == "?")
            mp[var[i]] = oper(bit, bit, op[i]);
          else if (operand[i][1] != "?" && operand[i][2] != "?")
            mp[var[i]] = oper(mp[operand[i][1]], mp[operand[i][2]], op[i]);
          else {
            if (operand[i][1] == "?")
              mp[var[i]] = oper(bit, mp[operand[i][2]], op[i]);
            else
              mp[var[i]] = oper(bit, mp[operand[i][1]], op[i]);
          }
        }
        if (mp[var[i]]) cnt[bit]++;
      }
    }
    if (cnt[0] == cnt[1])
      ans_min[p] = ans_max[p] = '0';
    else if (cnt[0] > cnt[1])
      ans_min[p] = '1', ans_max[p] = '0';
    else
      ans_min[p] = '0', ans_max[p] = '1';
  }
  cout << ans_min << "\n";
  cout << ans_max << "\n";
  return 0;
}
