#include <bits/stdc++.h>
using namespace std;
const int maxn = 5100, maxm = 1100;
int n, m, num[maxn][maxm], t0[maxn], t1[maxn];
map<string, int> nameid;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  string name, tmp, first, op, second;
  for (int i = 1; i <= (n); ++i) {
    cin >> name >> tmp >> first;
    nameid[name] = i;
    if (isdigit(first[0])) {
      for (int j = 1; j <= (m); ++j) num[i][j] = first[j - 1] - '0';
    } else {
      cin >> op >> second;
      int o;
      if (op == "AND")
        o = 1;
      else if (op == "OR")
        o = 2;
      else if (op == "XOR")
        o = 3;
      num[i][0] = o;
      if (first == "?")
        num[i][1] = 0;
      else
        num[i][1] = nameid[first];
      if (second == "?")
        num[i][2] = 0;
      else
        num[i][2] = nameid[second];
    }
  }
  vector<int> mi, mx;
  for (int pos = 1; pos <= (m); ++pos) {
    memset(t0, 0, sizeof(t0));
    memset(t1, 0, sizeof(t1));
    int c0 = 0, c1 = 0;
    t0[0] = 0;
    t1[0] = 1;
    for (int i = 1; i <= (n); ++i) {
      if (num[i][0] == 0) {
        t0[i] = t1[i] = num[i][pos];
      } else {
        if (num[i][0] == 1) {
          t0[i] = t0[num[i][1]] & t0[num[i][2]];
          t1[i] = t1[num[i][1]] & t1[num[i][2]];
        } else if (num[i][0] == 2) {
          t0[i] = t0[num[i][1]] | t0[num[i][2]];
          t1[i] = t1[num[i][1]] | t1[num[i][2]];
        } else if (num[i][0] == 3) {
          t0[i] = t0[num[i][1]] ^ t0[num[i][2]];
          t1[i] = t1[num[i][1]] ^ t1[num[i][2]];
        }
      }
      if (t0[i]) ++c0;
      if (t1[i]) ++c1;
    }
    if (c0 > c1) {
      mi.push_back(1);
      mx.push_back(0);
    } else if (c0 < c1) {
      mx.push_back(1);
      mi.push_back(0);
    } else {
      mx.push_back(0);
      mi.push_back(0);
    }
  }
  for (int i = 0; i < mi.size(); ++i) cout << mi[i];
  cout << endl;
  for (int i = 0; i < mx.size(); ++i) cout << mx[i];
  cout << endl;
  return 0;
}
