#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000, 102400000");
const int N = 5e3 + 10;
const int M = 1e3 + 10;
int n, m, x, y;
int a[N][M], t[N], ta[N], tb[N], aa[N], bb[N], f[N];
string s;
map<string, int> mp;
int check(int p, int k) {
  int sum = 0;
  f[0] = k;
  for (int i = 1; i <= n; i++) {
    if (t[i] == 0) f[i] = a[i][p];
    if (t[i] == 1) f[i] = f[ta[i]] & f[tb[i]];
    if (t[i] == 2) f[i] = f[ta[i]] | f[tb[i]];
    if (t[i] == 3) f[i] = f[ta[i]] ^ f[tb[i]];
    sum += f[i];
  }
  return sum;
}
int main() {
  mp["?"] = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    cin >> s;
    mp[s] = i;
    cin >> s;
    cin >> s;
    if (s[0] == '0' || s[0] == '1') {
      for (int j = 0; j <= m - 1; j++) {
        a[i][j] = s[j] - '0';
      }
      continue;
    }
    ta[i] = mp[s];
    cin >> s;
    if (s[0] == 'A')
      t[i] = 1;
    else if (s[0] == 'O')
      t[i] = 2;
    else
      t[i] = 3;
    cin >> s;
    tb[i] = mp[s];
  }
  for (int i = 0; i <= m - 1; i++) {
    x = check(i, 0);
    y = check(i, 1);
    aa[i] = y < x;
    bb[i] = x < y;
  }
  for (int i = 0; i <= m - 1; i++) printf("%d", aa[i]);
  printf("\n");
  for (int i = 0; i <= m - 1; i++) printf("%d", bb[i]);
  printf("\n");
  return 0;
}
