#include <bits/stdc++.h>
using namespace std;
int N;
int A[5005][3], val[5005];
string a, b, c, d, e, S[5005], minAns, maxAns;
map<string, int> X;
int getOp() {
  if (d == "AND") return 0;
  if (d == "OR") return 1;
  return 2;
}
int getVal(int i, int x) {
  int ans = 0;
  for (int j = 1; j <= N; j++) {
    if (A[j][0] == 0)
      val[j] = (S[j][i] - '0');
    else {
      int x1 = (A[j][0] == -1 ? x : val[A[j][0]]);
      int x2 = (A[j][1] == -1 ? x : val[A[j][1]]);
      if (A[j][2] == 0)
        val[j] = (x1 & x2);
      else if (A[j][2] == 1)
        val[j] = (x1 | x2);
      else
        val[j] = (x1 ^ x2);
    }
    ans += val[j];
  }
  return ans;
}
int main() {
  int M;
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; i++) {
    cin >> a >> b >> c;
    X[a] = i;
    if (c[0] == '0' || c[0] == '1')
      S[i] = c;
    else {
      cin >> d >> e;
      if (c == "?")
        A[i][0] = -1;
      else
        A[i][0] = X[c];
      if (e == "?")
        A[i][1] = -1;
      else
        A[i][1] = X[e];
      A[i][2] = getOp();
    }
  }
  for (int i = 0; i < M; i++) {
    int x = getVal(i, 0);
    int y = getVal(i, 1);
    if (x == y) {
      minAns.push_back('0');
      maxAns.push_back('0');
    } else if (x < y) {
      minAns.push_back('0');
      maxAns.push_back('1');
    } else {
      minAns.push_back('1');
      maxAns.push_back('0');
    }
  }
  cout << minAns << "\n" << maxAns << "\n";
  return 0;
}
