#include <bits/stdc++.h>
using namespace std;
const int X = 5009;
const long long inf = (1ll << 60);
long long n, m;
map<string, int> M;
string op[X], val[X], MN, MX;
int l[X], r[X];
int bit(int b, bool a) {
  if (a) val[n][b] = '1';
  vector<int> vec;
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (r[i] == -1) {
      ret += val[i][b] == '1';
      continue;
    }
    if (op[i] == "AND") {
      bool tmp = ((val[l[i]][b] == '1') & (val[r[i]][b] == '1'));
      ret += tmp;
      if (tmp) {
        val[i][b] = '1';
        vec.push_back(i);
      }
    }
    if (op[i] == "XOR") {
      bool tmp = ((val[l[i]][b] == '1') ^ (val[r[i]][b] == '1'));
      ret += tmp;
      if (tmp) {
        val[i][b] = '1';
        vec.push_back(i);
      }
    }
    if (op[i] == "OR") {
      bool tmp = ((val[l[i]][b] == '1') | (val[r[i]][b] == '1'));
      ret += tmp;
      if (tmp) {
        val[i][b] = '1';
        vec.push_back(i);
      }
    }
  }
  for (int i = 0; i < vec.size(); i++) val[vec[i]][b] = '0';
  val[n][b] = '0';
  return ret;
}
string solve(string s) {
  reverse(s.begin(), s.end());
  while (s.size() < m) s += '0';
  return s;
}
void input(int i) {
  string x, y, f, s;
  cin >> x >> y >> f;
  M[x] = i;
  if (f == "?" || M.find(f) != M.end()) {
    if (f == "?")
      l[i] = n;
    else
      l[i] = M[f];
    cin >> y >> s;
    op[i] = y;
    if (s == "?")
      r[i] = n;
    else
      r[i] = M[s];
    while (val[i].size() < m) val[i] += '0';
    return;
  }
  val[i] = solve(f);
  l[i] = r[i] = -1;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) input(i);
  while (val[n].size() < m) {
    val[n] += '0';
    MX += '0';
    MN += '0';
  }
  for (int i = 0; i < m; i++) {
    int a = bit(i, 0), b = bit(i, 1);
    if (a == b) continue;
    if (a > b) {
      MN[i] = '1';
      MX[i] = '0';
    }
    if (a < b) {
      MX[i] = '1';
      MN[i] = '0';
    }
  }
  reverse(MX.begin(), MX.end());
  reverse(MN.begin(), MN.end());
  cout << MN << endl << MX << endl;
}
