#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& num) {
  bool start = false, neg = false;
  char c;
  num = 0;
  while ((c = getchar()) != EOF) {
    if (c == '-')
      start = neg = true;
    else if (c >= '0' && c <= '9') {
      start = true;
      num = num * 10 + c - '0';
    } else if (start)
      break;
  }
  if (neg) num = -num;
}
int ai[5005], bi[5005], opt[5005];
int a[5005][1005], res[2];
int mi[5005], mx[5005];
char s[5005];
map<string, int> hs;
inline int calc(int a, int b, int c) {
  if (c == 1)
    return a & b;
  else if (c == 2)
    return a | b;
  return a ^ b;
}
int main() {
  int n, m;
  read(n);
  read(m);
  for (int i = (1); i <= (n); i++) {
    scanf("%s", s);
    string w = s;
    hs[w] = i;
    scanf("%s", s);
    scanf("%s", s);
    if (s[0] == '?' || isalpha(s[0])) {
      string w = s;
      if (s[0] == '?')
        ai[i] = n + 1;
      else
        ai[i] = hs[w];
      scanf("%s", s);
      if (s[0] == 'A')
        opt[i] = 1;
      else if (s[0] == 'O')
        opt[i] = 2;
      else if (s[0] == 'X')
        opt[i] = 3;
      scanf("%s", s);
      w = s;
      if (s[0] == '?')
        bi[i] = n + 1;
      else
        bi[i] = hs[w];
    } else {
      for (int j = (1); j <= (m); j++) a[i][j] = (s[j - 1] == '1');
    }
  }
  for (int k = (1); k <= (m); k++) {
    res[0] = res[1] = 0;
    for (int c = (0); c <= (1); c++) {
      a[n + 1][k] = c;
      for (int i = (1); i <= (n); i++) {
        if (!opt[i])
          res[c] += a[i][k];
        else
          a[i][k] = calc(a[ai[i]][k], a[bi[i]][k], opt[i]), res[c] += a[i][k];
      }
    }
    if (res[0] >= res[1])
      mx[k] = 0;
    else
      mx[k] = 1;
    if (res[0] <= res[1])
      mi[k] = 0;
    else
      mi[k] = 1;
  }
  for (int i = (1); i <= (m); i++) putchar(mi[i] + '0');
  putchar('\n');
  for (int i = (1); i <= (m); i++) putchar(mx[i] + '0');
  putchar('\n');
  return 0;
}
