#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
const int M = 1005;
const int K = 15;
int n, m;
char name[N][K], oper[N][K], val[N][M];
bool isV[N];
int idx1[N], idx2[N];
map<string, int> mp;
char ansmx[M], ansmn[M];
int res[N];
int work(int a, int b, char op[]) {
  if (op[0] == 'A') return a & b;
  if (op[0] == 'O') return a | b;
  return a ^ b;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s", name[i]);
    mp[name[i]] = i;
    char buf[M];
    scanf("%s", buf);
    scanf("%s", buf);
    if (buf[0] >= '0' && buf[0] <= '9') {
      memcpy(val[i], buf, sizeof(buf));
      isV[i] = 0;
    } else {
      isV[i] = 1;
      idx1[i] = buf[0] == '?' ? -1 : mp[buf];
      scanf("%s", oper[i]);
      scanf("%s", buf);
      idx2[i] = buf[0] == '?' ? -1 : mp[buf];
    }
  }
  for (int i = 0; i < m; i++) {
    int ans[2] = {0, 0};
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < n; k++) {
        if (!isV[k])
          res[k] = (val[k][i] - '0');
        else {
          int op1 = idx1[k] == -1 ? j : res[idx1[k]];
          int op2 = idx2[k] == -1 ? j : res[idx2[k]];
          res[k] = work(op1, op2, oper[k]);
        }
        ans[j] += res[k];
      }
    }
    if (ans[0] == ans[1])
      ansmx[i] = ansmn[i] = '0';
    else {
      ansmx[i] = ans[0] > ans[1] ? '0' : '1';
      ansmn[i] = ans[0] < ans[1] ? '0' : '1';
    }
  }
  printf("%s\n%s\n", ansmn, ansmx);
  return 0;
}
