#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int INF = 0x3f3f3f3f;
template <class T>
inline bool RD(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) return 0;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1, ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <class T>
inline void PT(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) PT(x / 10);
  putchar(x % 10 + '0');
}
const int N = 5e3 + 100;
int n, m;
map<string, int> mp;
string val[N];
int op[N];
pair<int, int> es[N];
char s[N];
int cnt;
pair<string, string> ans;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    mp[(string)s] = ++cnt;
    scanf("%s", s);
    scanf("%s", s);
    if (s[0] == '0' || s[0] == '1') {
      val[i] = (string)s;
    } else {
      val[i].resize(m);
      if (s[0] == '?')
        es[i].first = 0;
      else
        es[i].first = mp[(string)s];
      scanf("%s", s);
      if (s[0] == 'A')
        op[i] = 1;
      else if (s[0] == 'O')
        op[i] = 2;
      else
        op[i] = 3;
      scanf("%s", s);
      if (s[0] == '?')
        es[i].second = 0;
      else
        es[i].second = mp[(string)s];
    }
  }
  val[0].resize(m);
  for (int bit = 0; bit < m; bit++) {
    pair<int, int> c = pair<int, int>(0, 0);
    val[0][bit] = '0';
    for (int i = 1; i <= n; i++) {
      if (op[i] == 0)
        continue;
      else {
        int u = val[es[i].first][bit] - '0';
        int v = val[es[i].second][bit] - '0';
        if (op[i] == 1) val[i][bit] = (u & v) + '0';
        if (op[i] == 2) val[i][bit] = (u | v) + '0';
        if (op[i] == 3) val[i][bit] = (u ^ v) + '0';
        c.first += val[i][bit] == '1';
      }
    }
    val[0][bit] = '1';
    for (int i = 1; i <= n; i++) {
      if (op[i] == 0)
        continue;
      else {
        int u = val[es[i].first][bit] - '0';
        int v = val[es[i].second][bit] - '0';
        if (op[i] == 1) val[i][bit] = (u & v) + '0';
        if (op[i] == 2) val[i][bit] = (u | v) + '0';
        if (op[i] == 3) val[i][bit] = (u ^ v) + '0';
        c.second += val[i][bit] == '1';
      }
    }
    if (c.first == min(c.first, c.second))
      ans.first += '0';
    else
      ans.first += '1';
    if (c.first == max(c.first, c.second))
      ans.second += '0';
    else
      ans.second += '1';
  }
  printf("%s\n%s\n", ans.first.c_str(), ans.second.c_str());
}
