#include <bits/stdc++.h>
using namespace std;
struct T {
  int var, op1, op, op2;
};
string H[100011];
int val[100011];
map<string, int> M;
bool is_variable(int s) {
  if (s > 4 and H[s][0] != '0' and H[s][0] != '1') return 1;
  return 0;
}
int BIT(int &op1, int &op, int &op2, int cur, int j) {
  int v1, v2;
  if (is_variable(op1)) {
    v1 = val[op1];
  } else if (op1 == 4) {
    v1 = cur;
  } else {
    v1 = H[op1][j];
  }
  if (op2 == -1) return v1;
  if (is_variable(op2)) {
    v2 = val[op2];
  } else if (op2 == 4) {
    v2 = cur;
  } else {
    v2 = H[op2][j];
  }
  if (op == 1) return (v1 & v2);
  if (op == 2) return (v1 | v2);
  return (v1 ^ v2);
}
char s[30011];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<T> E;
  int cnt = 0;
  M[""] = -1;
  M["AND"] = 1;
  H[1] = "AND";
  M["OR"] = 2;
  H[2] = "OR";
  M["XOR"] = 3;
  H[3] = "XOR";
  M["?"] = 4;
  H[4] = "?";
  cnt = 5;
  for (int i = 0; i < n; i++) {
    cin.getline(s, 3000);
    if (strlen(s) == 0) {
      i--;
      continue;
    }
    string var = "";
    string op1 = "";
    string op2 = "";
    string op = "";
    int phase = 0;
    char c;
    int nm = strlen(s);
    for (int p = 0; p < nm; p++) {
      c = s[p];
      if (c == '\n') break;
      if (c == ' ') {
        phase++;
      } else {
        if (phase == 0) {
          var += c;
        } else if (phase == 1) {
        } else if (phase == 2) {
          op1 += c;
        } else if (phase == 3) {
          op += c;
        } else {
          op2 += c;
        }
      }
    }
    M[var] = cnt++;
    H[cnt - 1] = var;
    if (M.find(op1) == M.end()) {
      M[op1] = cnt++;
      H[cnt - 1] = op1;
    }
    if (M.find(op2) == M.end()) {
      M[op2] = cnt++;
      H[cnt - 1] = op2;
    }
    E.push_back({M[var], M[op1], M[op], M[op2]});
  }
  string m1 = "";
  string m2 = "";
  for (int j = 0; j < m; j++) {
    int one1 = 0;
    for (auto e : E) {
      val[e.var] = BIT(e.op1, e.op, e.op2, 0, j);
    }
    for (int k = 0; k < cnt; k++) one1 += val[k];
    int one2 = 0;
    for (auto e : E) {
      val[e.var] = BIT(e.op1, e.op, e.op2, 1, j);
    }
    for (int k = 0; k < cnt; k++) one2 += val[k];
    if (one1 == one2) {
      m1 += '0';
      m2 += '0';
    } else if (one1 < one2) {
      m1 += '0';
      m2 += '1';
    } else {
      m1 += '1';
      m2 += '0';
    }
  }
  cout << m1 << "\n" << m2;
}
