#include <bits/stdc++.h>
using namespace std;
const int NMax = 5e3 + 5;
const int MMax = 1e3 + 5;
int N, M;
map<string, int> mp;
bitset<MMax> bits[NMax][2];
int nr1By1[MMax], nr1By0[MMax];
int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  mp["?"] = 0;
  bits[0][1].set();
  for (int i = 1; i <= N; ++i) {
    string name, eq, op1;
    cin >> name >> eq >> op1;
    mp[name] = i;
    if (op1[0] == '0' || op1[0] == '1') {
      for (int j = 0; j < M; ++j) {
        bits[i][1].set(j, op1[j] - '0');
        bits[i][0].set(j, op1[j] - '0');
      }
    } else {
      string tor, op2;
      cin >> tor >> op2;
      int id1 = mp[op1], id2 = mp[op2];
      if (tor == "AND") {
        for (int b = 0; b < 2; ++b) {
          bits[i][b] = bits[id1][b] & bits[id2][b];
        }
      } else if (tor == "OR") {
        for (int b = 0; b < 2; ++b) {
          bits[i][b] = bits[id1][b] | bits[id2][b];
        }
      } else {
        for (int b = 0; b < 2; ++b) {
          bits[i][b] = bits[id1][b] ^ bits[id2][b];
        }
      }
      for (int b = 0; b < M; ++b) {
        nr1By1[b] += bits[i][1].test(b);
        nr1By0[b] += bits[i][0].test(b);
      }
    }
  }
  for (int b = 0; b < M; ++b) {
    int res = (nr1By1[b] < nr1By0[b]);
    cout << res;
  }
  cout << '\n';
  for (int b = 0; b < M; ++b) {
    int res = (nr1By1[b] > nr1By0[b]);
    cout << res;
  }
  return 0;
}
