#include <bits/stdc++.h>
using namespace std;
int read() {
  int w = 0, f = 1;
  char c = ' ';
  while (c < '0' || c > '9') c = getchar(), f = c == '-' ? -1 : f;
  while (c >= '0' && c <= '9') w = w * 10 + c - 48, c = getchar();
  return w * f;
}
struct node {
  string name;
  bitset<1005> b;
  int ty, x, y;
} a[5005];
map<string, int> mp;
int n, m, ans1[1005], ans2[1005];
string tmp, w = "?";
signed main() {
  cin >> n >> m;
  mp[w] = 5005 - 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].name >> tmp;
    mp[a[i].name] = i;
    cin >> tmp;
    if (tmp[0] == '1' || tmp[0] == '0') {
      for (int j = 0; j < m; j++) a[i].b[j] = tmp[j] - '0';
    } else {
      a[i].x = mp[tmp];
      cin >> tmp;
      if (tmp == "AND")
        a[i].ty = 1;
      else if (tmp == "OR")
        a[i].ty = 2;
      else
        a[i].ty = 3;
      cin >> tmp;
      a[i].y = mp[tmp];
    }
  }
  for (int i = 0; i < m; i++) {
    int res1 = 0, res2 = 0;
    a[5005 - 1].b[i] = 0;
    for (int j = 1; j <= n; j++) {
      int x = a[j].x, y = a[j].y;
      if (a[j].ty == 1) a[j].b[i] = a[x].b[i] & a[y].b[i];
      if (a[j].ty == 2) a[j].b[i] = a[x].b[i] | a[y].b[i];
      if (a[j].ty == 3) a[j].b[i] = a[x].b[i] ^ a[y].b[i];
      res1 += a[j].b[i];
    }
    a[5005 - 1].b[i] = 1;
    for (int j = 1; j <= n; j++) {
      int x = a[j].x, y = a[j].y;
      if (a[j].ty == 1) a[j].b[i] = a[x].b[i] & a[y].b[i];
      if (a[j].ty == 2) a[j].b[i] = a[x].b[i] | a[y].b[i];
      if (a[j].ty == 3) a[j].b[i] = a[x].b[i] ^ a[y].b[i];
      res2 += a[j].b[i];
    }
    if (res1 == res2)
      ans1[i] = ans2[i] = 0;
    else if (res1 < res2)
      ans1[i] = 0, ans2[i] = 1;
    else
      ans1[i] = 1, ans2[i] = 0;
  }
  for (int i = 0; i < m; i++) putchar('0' + ans1[i]);
  putchar('\n');
  for (int i = 0; i < m; i++) putchar('0' + ans2[i]);
  putchar('\n');
  return 0;
}
