#include <bits/stdc++.h>
using namespace std;
map<string, int> id;
int n, m, kind[5010], val[5010][1010], opt[5010], opta[5010], optb[5010],
    mxa[1010], mna[1010];
int calc(int p) {
  int ret = 0;
  for (int i = 1; i <= n; i++) {
    if (kind[i] == 1) val[i][p] = (val[opta[i]][p] & val[optb[i]][p]);
    if (kind[i] == 2) val[i][p] = (val[opta[i]][p] | val[optb[i]][p]);
    if (kind[i] == 3) val[i][p] = (val[opta[i]][p] ^ val[optb[i]][p]);
    ret += val[i][p];
  }
  return ret;
}
int main() {
  int x0, x1;
  string s;
  ios::sync_with_stdio(0);
  cin >> n >> m;
  id["?"] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    id[s] = i;
    cin >> s;
    cin >> s;
    if (s[0] == '0' || s[0] == '1') {
      kind[i] = 0;
      for (int j = 1; j <= m; j++) val[i][j] = s[j - 1] - '0';
    } else {
      opta[i] = id[s];
      cin >> s;
      if (s[0] == 'A')
        kind[i] = 1;
      else {
        if (s[0] == 'O')
          kind[i] = 2;
        else
          kind[i] = 3;
      }
      cin >> s;
      optb[i] = id[s];
    }
  }
  for (int j = 1; j <= m; j++) {
    val[0][j] = 0;
    x0 = calc(j);
    val[0][j] = 1;
    x1 = calc(j);
    if (x0 == x1)
      mxa[j] = mna[j] = 0;
    else {
      if (x0 < x1) {
        mna[j] = 0;
        mxa[j] = 1;
      } else {
        mna[j] = 1;
        mxa[j] = 0;
      }
    }
  }
  for (int j = 1; j <= m; j++) cout << mna[j];
  cout << endl;
  for (int j = 1; j <= m; j++) cout << mxa[j];
  cout << endl;
}
