#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
int n, m, a[5009], b[5009], c[5009];
int mx[1009], mn[1009];
bool val[5009][1009];
char s[5009];
map<string, int> mapa;
int eval(int j, int v) {
  int r = 0;
  for (int(i) = (0); (i) < (n); ++(i))
    if (c[i] != -1) {
      bool oa = (a[i] == -1) ? v : val[a[i]][j];
      bool ob = (b[i] == -1) ? v : val[b[i]][j];
      val[i][j] = (c[i] == 0)   ? (oa && ob)
                  : (c[i] == 1) ? (oa || ob)
                                : (oa ^ ob);
      r += val[i][j];
    }
  return r;
}
void proc(int i) {
  int q0 = eval(i, 0);
  int q1 = eval(i, 1);
  if (q0 < q1) {
    mn[i] = 0;
    mx[i] = 1;
  } else if (q0 > q1) {
    mn[i] = 1;
    mx[i] = 0;
  } else {
    mn[i] = 0;
    mx[i] = 0;
  }
}
int main() {
  while (scanf("%d%d", &n, &m) == 2) {
    mapa.clear();
    mapa["?"] = -1;
    for (int(i) = (0); (i) < (n); ++(i)) {
      scanf("%s", s);
      string ss(s);
      mapa[ss] = i;
      scanf("%s", s);
      scanf("%s", s);
      if (isdigit(s[0])) {
        c[i] = -1;
        for (int(j) = (0); (j) < (m); ++(j)) val[i][j] = (s[j] == '1');
      } else {
        a[i] = mapa[string(s)];
        scanf("%s", s);
        c[i] = (s[0] == 'A') ? 0 : (s[0] == 'O') ? 1 : 2;
        scanf("%s", s);
        b[i] = mapa[string(s)];
      }
    }
    for (int(i) = (0); (i) < (m); ++(i)) proc(i);
    for (int(i) = (0); (i) < (m); ++(i)) putchar(mn[i] ? '1' : '0');
    puts("");
    for (int(i) = (0); (i) < (m); ++(i)) putchar(mx[i] ? '1' : '0');
    puts("");
  }
  return 0;
}
