#include <bits/stdc++.h>
using namespace std;
struct vals {
  bool vd;
  char val[1010];
  bool cur;
  int idx[2];
  string nome, op;
} s[5020];
bool valdef(bool k, int vv, int curidx) {
  if (s[curidx].idx[vv] == -1) return k;
  return s[s[curidx].idx[vv]].cur;
}
bool value(string op, bool k, int curidx) {
  if (op == "XOR") {
    return valdef(k, 0, curidx) ^ valdef(k, 1, curidx);
  } else if (op == "OR") {
    return valdef(k, 0, curidx) | valdef(k, 1, curidx);
  } else if (op == "AND") {
    return valdef(k, 0, curidx) & valdef(k, 1, curidx);
  }
}
int main() {
  int n, m;
  char c;
  char aux1[12], aux2[1010], aux3[5], aux4[12];
  map<string, int> mpi;
  cin >> n >> m;
  for (int i = 0; i < (n); ++i) {
    scanf("%s := %s%c", aux1, aux2, &c);
    mpi[string(aux1)] = i;
    if (c == ' ') {
      scanf("%s %s", aux3, aux4);
      if (string(aux2) != "?")
        s[i].idx[0] = mpi[string(aux2)];
      else
        s[i].idx[0] = -1;
      s[i].op = string(aux3);
      if (string(aux4) != "?")
        s[i].idx[1] = mpi[string(aux4)];
      else
        s[i].idx[1] = -1;
      s[i].vd = false;
    } else if (c == '\n' or c == '\r') {
      strcpy(s[i].val, aux2);
      s[i].vd = true;
    } else
      cout << "erro" << endl;
  }
  string sauxmax = "";
  string sauxmin = "";
  for (int i = 0; i < (m); ++i) {
    vector<int> vsoma(2, 0);
    for (int k = 0; k < (2); ++k) {
      for (int j = 0; j < (n); ++j) {
        bool baux;
        if (s[j].vd) {
          baux = s[j].val[i] - '0';
          s[j].cur = baux;
        } else {
          baux = value(s[j].op, k, j);
          s[j].cur = baux;
        }
        vsoma[k] += baux;
      }
    }
    if (vsoma[1] > vsoma[0])
      sauxmax += '1', sauxmin += '0';
    else if (vsoma[1] == vsoma[0])
      sauxmax += '0', sauxmin += '0';
    else if (vsoma[1] < vsoma[0])
      sauxmax += '0', sauxmin += '1';
  }
  cout << sauxmin << endl << sauxmax << endl;
}
