#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5000;
int n, m;
string inp[MAXN + 10];
char mn[MAXN + 10], mx[MAXN + 10];
int bc[MAXN + 10][MAXN + 10];
int aa[MAXN + 10], bb[MAXN + 10];
string cmd[MAXN + 10];
map<string, int> id;
int bc_with(int j, int v) {
  int rs = 0;
  for (int i = 0; i < n; i++) {
    if (cmd[i] == "") {
      rs += bc[i][j];
    } else {
      int bA = aa[i] == -1 ? v : bc[aa[i]][j];
      int bB = bb[i] == -1 ? v : bc[bb[i]][j];
      if (cmd[i] == "XOR") {
        bc[i][j] = bA ^ bB;
      } else if (cmd[i] == "OR") {
        bc[i][j] = bA | bB;
      } else if (cmd[i] == "AND") {
        bc[i][j] = bA & bB;
      }
      rs += bc[i][j];
    }
  }
  return rs;
}
void work() {
  memset(mn, 0, sizeof(mn));
  memset(mx, 0, sizeof(mx));
  for (int j = 0; j < m; j++) {
    int rz = bc_with(j, 0), ro = bc_with(j, 1);
    if (rz == ro) {
      mn[j] = mx[j] = '0';
    } else if (rz < ro) {
      mn[j] = '0';
      mx[j] = '1';
    } else {
      mn[j] = '1';
      mx[j] = '0';
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  cin.ignore();
  id["?"] = -1;
  memset(bc, -1, sizeof(bc));
  for (int i = 0; i < n; i++) {
    string nm;
    cin >> nm;
    id[nm] = i;
    string ss;
    cin >> ss;
    cin >> ss;
    if (ss[0] == '0' or ss[0] == '1') {
      for (int j = 0; j < m; j++) {
        bc[i][j] = ss[j] == '1';
      }
      cmd[i] = "";
    } else {
      string op;
      string B;
      cin >> op >> B;
      aa[i] = id[ss];
      cmd[i] = op;
      bb[i] = id[B];
    }
  }
  work();
  cout << mn << "\n" << mx << endl;
}
