#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
bitset<1003> bs[5005][2];
int res[1003][2];
int main() {
  int n, m;
  cin >> n >> m;
  mp["?"] = 0;
  bs[0][1].set();
  string x, tmp, y, op, z;
  getchar();
  for (int i = 0; i < n; ++i) {
    cin >> x >> tmp >> y;
    mp[x] = i + 1;
    if (y[0] == '1' || y[0] == '0') {
      for (int j = 0; j < m; j++) {
        bs[i + 1][0].set(j, y[j] - '0');
        bs[i + 1][1].set(j, y[j] - '0');
      }
    } else {
      cin >> op >> z;
      for (int b = 0; b < 2; b++) {
        switch (op[0]) {
          case 'A':
            bs[i + 1][b] = bs[mp[y]][b] & bs[mp[z]][b];
            break;
          case 'X':
            bs[i + 1][b] = bs[mp[y]][b] ^ bs[mp[z]][b];
            break;
          case 'O':
            bs[i + 1][b] = bs[mp[y]][b] | bs[mp[z]][b];
            break;
        }
      }
    }
    for (int j = 0; j < m; j++) {
      res[j][0] += bs[i + 1][0][j];
      res[j][1] += bs[i + 1][1][j];
    }
  }
  for (int i = 0; i < m; i++) {
    cout << (res[i][0] > res[i][1] ? 1 : 0);
  }
  puts("");
  for (int i = 0; i < m; i++) {
    cout << (res[i][0] >= res[i][1] ? 0 : 1);
  }
  return 0;
}
