#include <bits/stdc++.h>
using namespace std;
void open() {
  freopen("D:/Code/in.txt", "r", stdin);
  freopen("D:/Code/out.txt", "w", stdout);
}
map<string, int> mapper;
map<int, string> rev;
int op[5020];
string val[2][5020];
int main(void) {
  std::ios::sync_with_stdio(false);
  int n, m;
  memset(op, -1, sizeof(op));
  cin >> n >> m;
  int ct = 0;
  for (int i = 0; i < n; i++) {
    string str, dum, baris;
    cin >> str >> dum;
    mapper[str] = i;
    getline(cin, baris);
    string zzz;
    int comp = 0;
    for (int z = 0; z < (int)baris.size(); z++) {
      if (baris[z] == ' ' && zzz.empty()) continue;
      if (baris[z] == ' ') {
        if (zzz[0] == '?') {
          if (comp == 0) val[0][i] = "---";
        } else if (zzz[0] == 'A') {
          op[i] = 0;
        } else if (zzz[0] == 'O') {
          op[i] = 1;
        } else if (zzz[0] == 'X') {
          op[i] = 2;
        } else {
          if (comp == 0) val[0][i] = zzz;
        }
        comp++;
        zzz.clear();
      } else
        zzz += baris[z];
    }
    if (zzz[0] == '?') {
      if (comp == 0)
        val[0][i] = "---";
      else
        val[1][i] = "---";
    } else {
      if (comp == 0)
        val[0][i] = zzz;
      else
        val[1][i] = zzz;
    }
  }
  string maxi, mini;
  for (int i = 0; i < m; i++) {
    int curval = 0;
    for (int a = 0; a < 2; a++) {
      int temp[5020];
      memset(temp, 0, sizeof(temp));
      for (int j = 0; j < n; j++) {
        if (op[j] == -1) {
          temp[j] = val[0][j][i] - '0';
        } else {
          int left, right;
          if (val[0][j][0] == '-') {
            left = a;
          } else {
            int idx = mapper[val[0][j]];
            left = temp[idx];
          }
          if (val[1][j][0] == '-') {
            right = a;
          } else {
            int idx = mapper[val[1][j]];
            right = temp[idx];
          }
          if (op[j] == 0) {
            temp[j] = (left & right);
          } else if (op[j] == 1) {
            temp[j] = (left | right);
          } else if (op[j] == 2) {
            temp[j] = (left ^ right);
          }
        }
      }
      int hasil = 0;
      for (int j = 0; j < n; j++) {
        assert(temp[j] < 2);
        hasil += temp[j];
      }
      if (a == 0) {
        curval = hasil;
        maxi += '0';
        mini += '0';
      } else {
        if (curval < hasil) {
          maxi[i] = '1';
        } else if (curval > hasil) {
          mini[i] = '1';
        }
      }
    }
  }
  assert((int)mini.size() == m && (int)maxi.size() == m);
  cout << mini << "\n" << maxi;
  return 0;
}
