#include <bits/stdc++.h>
using namespace std;
char v[5005][2][1005];
int n, m, opr1[5005], opr2[5005], opr[5005];
map<string, int> ci;
int f(int a, int o, int b) {
  if (o == 0) return (a % 2 ^ b % 2) + 48;
  if (o == 1) return (a % 2 & b % 2) + 48;
  return (a % 2 | b % 2) + 48;
}
int pa(int x, int num) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (v[i][num - 48][x] != '0' && v[i][num - 48][x] != '1') {
      if (opr1[i] != -1 && opr2[i] != -1)
        v[i][num - 48][x] =
            f(v[opr1[i]][num - 48][x], opr[i], v[opr2[i]][num - 48][x]);
      else if (opr1[i] == -1 && opr2[i] == -1)
        v[i][num - 48][x] = f(num, opr[i], num);
      else if (opr2[i] == -1)
        v[i][num - 48][x] = f(num, opr[i], v[opr1[i]][num - 48][x]);
      else {
        v[i][num - 48][x] = f(num, opr[i], v[opr2[i]][num - 48][x]);
      }
    }
    ans += v[i][num - 48][x] == '1';
  }
  return ans;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    char c[105], s[1005];
    scanf("%s := %s", c, s);
    ci[c] = i;
    if (s[0] == '0' || s[0] == '1') {
      strcpy(v[i][0], s);
      strcpy(v[i][1], s);
    } else {
      char op[10], s2[105];
      scanf("%s %s", op, s2);
      if (op[0] == 'X')
        opr[i] = 0;
      else if (op[0] == 'A')
        opr[i] = 1;
      else
        opr[i] = 2;
      if (s[0] != '?')
        opr1[i] = ci[s];
      else
        opr1[i] = -1;
      if (s2[0] != '?')
        opr2[i] = ci[s2];
      else
        opr2[i] = -1;
    }
  }
  char mi[10005], ma[10005];
  for (int i = 0; i < m; i++) {
    int n0 = pa(i, '0');
    int n1 = pa(i, '1');
    if (n0 > n1) {
      ma[i] = '0';
      mi[i] = '1';
    } else if (n0 == n1)
      ma[i] = mi[i] = '0';
    else {
      ma[i] = '1';
      mi[i] = '0';
    }
  }
  ma[m] = mi[m] = 0;
  printf("%s\n%s\n", mi, ma);
  return 0;
}
