#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5005;
const int MAX_M = 1005;
struct NODE {
  int l, op, r;
  char num[MAX_M];
};
int N, M;
int mi[MAX_M], ma[MAX_M], v[MAX_N];
map<string, int> mp;
NODE node[MAX_N];
int calc(int pos, int val) {
  int ret = 0;
  memset(v, 0, sizeof(v));
  for (int i = 0; i < N; ++i) {
    if (node[i].op == -1) {
      v[i] = node[i].num[pos] == '1';
    } else {
      int opl = node[i].l == -1 ? val : v[node[i].l];
      int opr = node[i].r == -1 ? val : v[node[i].r];
      if (node[i].op == 0)
        v[i] = opl & opr;
      else if (node[i].op == 1)
        v[i] = opl | opr;
      else
        v[i] = opl ^ opr;
    }
    ret += v[i];
  }
  return ret;
}
int main() {
  scanf("%d%d", &N, &M);
  char tmpc[MAX_M];
  for (int i = 0; i < N; ++i) {
    scanf("%s", tmpc);
    mp[(string)tmpc] = i;
    scanf("%*s%s", tmpc);
    if (tmpc[0] == '0' || tmpc[0] == '1') {
      node[i].op = -1;
      strcpy(node[i].num, tmpc);
    } else {
      node[i].l = tmpc[0] == '?' ? -1 : mp[(string)tmpc];
      scanf("%s", tmpc);
      if (tmpc[0] == 'A')
        node[i].op = 0;
      else if (tmpc[0] == 'O')
        node[i].op = 1;
      else
        node[i].op = 2;
      scanf("%s", tmpc);
      node[i].r = tmpc[0] == '?' ? -1 : mp[(string)tmpc];
    }
  }
  for (int i = 0; i < M; ++i) {
    int na = calc(i, 1);
    int nb = calc(i, 0);
    if (na > nb)
      mi[i] = 0, ma[i] = 1;
    else if (na < nb)
      mi[i] = 1, ma[i] = 0;
    else
      mi[i] = 0, ma[i] = 0;
  }
  for (int i = 0; i < M; ++i) printf("%d", mi[i]);
  printf("\n");
  for (int i = 0; i < M; ++i) printf("%d", ma[i]);
  printf("\n");
  return 0;
}
