#include <bits/stdc++.h>
using namespace std;
unsigned long long getHash(string &s) {
  unsigned long long h = 0;
  for (int i = 0; i < s.size(); i++) h = h * 31 + s[i] - 'a' + 1;
  return h;
}
unsigned long long H[5005];
vector<string> var[5005];
vector<unsigned long long> var_hash[5005];
unordered_map<unsigned long long, int> mapa;
string res[5005];
int op[5005], n;
int getBit(int pos, int j, int idx, int value) {
  if (var[pos][idx] == "?") return value;
  if (mapa.find(var_hash[pos][idx]) != mapa.end()) {
    int p = mapa[var_hash[pos][idx]];
    return res[p][j] - '0';
  }
  return var[pos][idx][j] - '0';
}
int countBitValue(int bitPos, int value, char target) {
  int cnt = 0, bit1, bit2, pos;
  for (int j = 0; j < n; j++) {
    if (op[j] == -1) {
      if (var[j][0] == "?") {
        res[j][bitPos] = value + '0';
      } else if (mapa.find(var_hash[j][0]) != mapa.end()) {
        pos = mapa[var_hash[j][0]];
        res[j][bitPos] = res[pos][bitPos];
      } else
        res[j][bitPos] = var[j][0][bitPos];
    } else {
      bit1 = getBit(j, bitPos, 0, value);
      bit2 = getBit(j, bitPos, 1, value);
      if (op[j] == 0) res[j][bitPos] = (bit1 ^ bit2) + '0';
      if (op[j] == 1) res[j][bitPos] = (bit1 | bit2) + '0';
      if (op[j] == 2) res[j][bitPos] = (bit1 & bit2) + '0';
    }
    if (res[j][bitPos] == target) cnt++;
  }
  return cnt;
}
int main() {
  ios::sync_with_stdio(false);
  int m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) res[i] = string(m, '0');
  string s, v;
  getline(cin, s);
  for (int i = 0; i < n; i++) {
    int type = -1;
    getline(cin, s);
    if (s.find("XOR") >= 0 && s.find("XOR") < s.size())
      type = 0;
    else if (s.find("OR") >= 0 && s.find("OR") < s.size())
      type = 1;
    else if (s.find("AND") >= 0 && s.find("AND") < s.size())
      type = 2;
    op[i] = type;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == ':' || s[j] == '=' || (s[j] >= 'A' && s[j] <= 'Z'))
        s[j] = ' ';
    }
    stringstream in(s);
    in >> v;
    H[i] = getHash(v);
    mapa[H[i]] = i;
    while (in >> v) var[i].push_back(v), var_hash[i].push_back(getHash(v));
  }
  int cnt1, cnt2;
  for (int i = 0; i < m; i++) {
    cnt1 = countBitValue(i, 0, '0');
    cnt2 = countBitValue(i, 1, '0');
    if (cnt1 >= cnt2)
      cout << 0;
    else
      cout << 1;
  }
  cout << endl;
  for (int i = 0; i < m; i++) {
    cnt1 = countBitValue(i, 0, '1');
    cnt2 = countBitValue(i, 1, '1');
    if (cnt1 >= cnt2)
      cout << 0;
    else
      cout << 1;
  }
  cout << endl;
}
