#include <bits/stdc++.h>
using namespace std;
map<string, int> M;
struct Node {
  int num1, num2, op;
  char x[1100];
} a[5100];
string s;
int n, m, cnt;
char s1[1100], s2[1100], s3[1100], s4[1100], ch;
int ans[2];
int minn[1100], maxn[1100];
int f[5100];
void clear() { memset(f, 0, sizeof(f)); }
void solve() {
  for (int i = 1; i <= m; i++) {
    ans[0] = 0;
    ans[1] = 0;
    for (int j = 0; j <= 1; j++) {
      clear();
      f[0] = j;
      for (int k = 1; k <= n; k++) {
        if (a[k].op == 0) {
          ans[j] += (f[k] = a[k].x[i] - '0');
        }
        if (a[k].op == 1) {
          ans[j] += (f[k] = f[a[k].num1]);
        }
        if (a[k].op == 2) {
          ans[j] += (f[k] = f[a[k].num1] & f[a[k].num2]);
        }
        if (a[k].op == 3) {
          ans[j] += (f[k] = f[a[k].num1] | f[a[k].num2]);
        }
        if (a[k].op == 4) {
          ans[j] += (f[k] = f[a[k].num1] ^ f[a[k].num2]);
        }
      }
    }
    if (ans[0] == ans[1]) minn[i] = maxn[i] = 0;
    if (ans[0] < ans[1]) minn[i] = 0, maxn[i] = 1;
    if (ans[1] < ans[0]) minn[i] = 1, maxn[i] = 0;
  }
}
int main() {
  M["?"] = 0;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s := %s", s1 + 1, s2 + 1);
    s = "";
    int len = strlen(s1 + 1);
    for (int j = 1; j <= len; j++) {
      s = s + s1[j];
    }
    M[s] = ++cnt;
    scanf("%c", &ch);
    if (ch == '\n') {
      if (s2[1] == '0' || s2[1] == '1') {
        len = strlen(s2 + 1);
        a[i].op = 0;
        for (int j = 1; j <= len; j++) {
          a[i].x[j] = s2[j];
        }
      } else {
        s = "";
        len = strlen(s2 + 1);
        a[i].op = 1;
        for (int j = 1; j <= len; j++) {
          s = s + s2[j];
        }
        a[i].num1 = M[s];
      }
    } else {
      scanf("%s %s\n", s3 + 1, s4 + 1);
      s = "";
      len = strlen(s2 + 1);
      for (int j = 1; j <= len; j++) {
        s = s + s2[j];
      }
      a[i].num1 = M[s];
      s = "";
      len = strlen(s4 + 1);
      for (int j = 1; j <= len; j++) {
        s = s + s4[j];
      }
      a[i].num2 = M[s];
      if (s3[1] == 'A') a[i].op = 2;
      if (s3[1] == 'O') a[i].op = 3;
      if (s3[1] == 'X') a[i].op = 4;
    }
  }
  solve();
  for (int i = 1; i <= m; i++) {
    printf("%d", minn[i]);
  }
  printf("\n");
  for (int i = 1; i <= m; i++) {
    printf("%d", maxn[i]);
  }
  return 0;
}
