#include <bits/stdc++.h>
using namespace std;
const unsigned long long sed = 173;
map<unsigned long long, int> id;
int val[6006][6006];
int opt[6006], fir[6006], sec[6006];
int n, m;
bool f[6006];
void read(int u) {
  char ch = getchar();
  while (ch == ' ' || ch == '\n') ch = getchar();
  unsigned long long name = 0;
  while (ch != ' ') {
    name = name * sed + ch - 'a' + 1;
    ch = getchar();
  }
  id[name] = u;
  ch = getchar();
  while (ch != ' ') ch = getchar();
  ch = getchar();
  if (ch == '0' || ch == '1') {
    val[u][1] = ch - '0';
    for (int i = 2; i <= m; i++) {
      ch = getchar();
      int x = ch - '0';
      val[u][i] = x;
    }
  } else {
    if (ch == '?')
      fir[u] = -1, ch = getchar();
    else {
      unsigned long long tmp = 0;
      while (ch != ' ') {
        tmp = tmp * sed + ch - 'a' + 1;
        ch = getchar();
      }
      fir[u] = id[tmp];
    }
    char op[10];
    scanf("%s", op + 1);
    if (op[1] == 'X')
      opt[u] = 1;
    else if (op[1] == 'O')
      opt[u] = 2;
    else
      opt[u] = 3;
    ch = getchar();
    ch = getchar();
    if (ch == '?')
      sec[u] = -1, ch = getchar();
    else {
      unsigned long long tmp = 0;
      while (ch != ' ' && ch != '\n') {
        tmp = tmp * sed + ch - 'a' + 1;
        ch = getchar();
      }
      sec[u] = id[tmp];
    }
    f[u] = 1;
  }
}
int mx[6006], mn[6006];
int cal(int u, int x) {
  int ret = 0;
  for (int i = 1; i <= n; i++) {
    if (!f[i]) continue;
    int a, b;
    if (fir[i] == -1)
      a = x;
    else
      a = val[fir[i]][u];
    if (sec[i] == -1)
      b = x;
    else
      b = val[sec[i]][u];
    if (opt[i] == 1)
      val[i][u] = a ^ b;
    else if (opt[i] == 2)
      val[i][u] = a | b;
    else
      val[i][u] = a & b;
    ret += val[i][u];
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) read(i);
  for (int i = 1; i <= m; i++) {
    int x = cal(i, 1);
    int y = cal(i, 0);
    if (x > y)
      mx[i] = 1, mn[i] = 0;
    else if (x == y)
      mx[i] = mn[i] = 0;
    else
      mx[i] = 0, mn[i] = 1;
  }
  for (int i = 1; i <= m; i++) printf("%d", mn[i]);
  printf("\n");
  for (int i = 1; i <= m; i++) printf("%d", mx[i]);
}
