#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
struct node {
  int op, l, r;
  string s;
} a[N];
map<string, int> idx;
string var, tmp, sl, op, sr, mn = "", mx = "";
int t[2][N], cnt[2], n, m;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> var >> tmp >> a[i].s;
    idx[var] = i;
    if (isdigit(a[i].s[0])) continue;
    cin >> op >> sr;
    if (op[0] == 'A')
      a[i].op = 1;
    else if (op[0] == 'O')
      a[i].op = 2;
    else if (op[0] == 'X')
      a[i].op = 3;
    a[i].l = (a[i].s == "?" ? n + 1 : idx[a[i].s]);
    a[i].r = (sr == "?" ? n + 1 : idx[sr]);
  }
  t[0][n + 1] = 0, t[1][n + 1] = 1;
  for (int i = 0; i < m; i++) {
    cnt[0] = 0, cnt[1] = 0;
    for (int k = 0; k <= 1; k++) {
      for (int j = 1; j <= n; j++) {
        if (a[j].op == 0)
          t[k][j] = a[j].s[i] - '0';
        else if (a[j].op == 1)
          t[k][j] = t[k][a[j].l] & t[k][a[j].r];
        else if (a[j].op == 2)
          t[k][j] = t[k][a[j].l] | t[k][a[j].r];
        else if (a[j].op == 3)
          t[k][j] = t[k][a[j].l] ^ t[k][a[j].r];
        if (t[k][j]) cnt[k]++;
      }
    }
    mn += cnt[1] >= cnt[0] ? '0' : '1';
    mx += cnt[1] > cnt[0] ? '1' : '0';
  }
  cout << mn << endl << mx << endl;
}
