#include <bits/stdc++.h>
using namespace std;
struct Exp {
  int id;
  string value;
  int s;
  int t;
  int op = 0;
};
Exp v[5005];
int n, m, c;
string s;
map<string, int> mp;
bool value[5005];
int test(int id, int c) {
  value[0] = c;
  int res = 0;
  ;
  for (int i = 0; i < n; i++) {
    Exp cur = v[i];
    bool temp = 0;
    if (cur.op == 0) {
      temp = cur.value[id] - '0';
    } else {
      int op1 = value[cur.s];
      int op2 = value[cur.t];
      if (cur.op == 1)
        temp = op1 & op2;
      else if (cur.op == 2)
        temp = op1 | op2;
      else if (cur.op == 3)
        temp = op1 ^ op2;
    }
    value[cur.id] = temp;
    res += temp;
  }
  return res;
}
int main() {
  scanf("%d %d\n", &n, &m);
  mp["?"] = 0;
  for (int i = 0; i < n; i++) {
    getline(cin, s);
    istringstream sin(s);
    string va;
    sin >> va;
    mp[va] = ++c;
    string temp;
    sin >> temp;
    sin >> temp;
    v[i].id = c;
    if (isdigit(temp[0])) {
      v[i].value = temp;
    } else {
      v[i].s = mp[temp];
      sin >> temp;
      if (temp == "AND")
        v[i].op = 1;
      else if (temp == "OR")
        v[i].op = 2;
      else if (temp == "XOR")
        v[i].op = 3;
      sin >> temp;
      v[i].t = mp[temp];
    }
  }
  char rmn[m + 1], rmx[m + 1];
  memset(rmn, 0, sizeof rmn);
  memset(rmx, 0, sizeof rmx);
  for (int i = 0; i < m; i++) {
    int r1 = test(i, 0);
    int r2 = test(i, 1);
    if (r1 >= r2)
      rmx[i] = '0';
    else
      rmx[i] = '1';
    if (r1 > r2)
      rmn[i] = '1';
    else
      rmn[i] = '0';
  }
  printf("%s\n", rmn);
  printf("%s\n", rmx);
  return 0;
}
