#include <bits/stdc++.h>
using namespace std;
const int INF = 987654321;
const int MOD = 1000000007;
int n, m;
map<string, int> my;
struct assign {
  int type;
  string value;
  string op;
  string aa, bb;
};
assign arr[5001];
int calc(int loc, int tp) {
  int ret = 0;
  for (int i = 0; i < (n); ++i) {
    if (arr[i].type == 0) {
      ret += arr[i].value[loc] - '0';
    } else {
      int a, b;
      if (arr[i].aa[0] == '?')
        a = tp;
      else {
        int idx = my[arr[i].aa];
        a = arr[idx].value[loc] - '0';
      }
      if (arr[i].bb[0] == '?')
        b = tp;
      else {
        int idx = my[arr[i].bb];
        b = arr[idx].value[loc] - '0';
      }
      int result;
      if (arr[i].op == "AND")
        result = a & b;
      else if (arr[i].op == "OR")
        result = a | b;
      else
        result = a ^ b;
      ret += result;
      arr[i].value[loc] = result + '0';
    }
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < (n); ++i) {
    string str;
    cin >> str;
    my[str] = i;
    cin >> str;
    cin >> str;
    if (str[0] == '1' || str[0] == '0') {
      arr[i].type = 0;
      arr[i].value = str;
    } else {
      arr[i].type = 1;
      arr[i].value.resize(m, '0');
      string a, o, b;
      a = str;
      cin >> o >> b;
      arr[i].aa = a, arr[i].op = o, arr[i].bb = b;
    }
  }
  string sol1, sol2;
  for (int i = 0; i < (m); ++i) {
    int outFromZero = calc(i, 0);
    int outFromOne = calc(i, 1);
    if (outFromZero < outFromOne) {
      sol1.push_back('0');
      sol2.push_back('1');
    } else if (outFromZero > outFromOne) {
      sol1.push_back('1');
      sol2.push_back('0');
    } else {
      sol1.push_back('0');
      sol2.push_back('0');
    }
  }
  cout << sol1 << '\n' << sol2;
  return 0;
}
