#include <bits/stdc++.h>
using namespace std;
void fre() {
  freopen("c://test//input.in", "r", stdin);
  freopen("c://test//output.out", "w", stdout);
}
template <class T1, class T2>
inline void gmax(T1 &a, T2 b) {
  if (b > a) a = b;
}
template <class T1, class T2>
inline void gmin(T1 &a, T2 b) {
  if (b < a) a = b;
}
const int N = 5010, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>
inline void gadd(T1 &a, T2 b) {
  a = (a + b) % Z;
}
int casenum, casei;
int n, m;
char a[N][12], b[N][12], val[N][N], op[N][10], V[N][N];
map<string, int> mop;
char ans1[N], ans2[N];
int oa[N], oval[N], ob[N];
int check(int p, int v) {
  int ret = 0;
  for (int i = 1; i <= n; ++i) {
    if (isdigit(val[i][0])) {
      ret += val[i][p] - '0';
    } else {
      int va, vb;
      if (val[i][0] == '?')
        va = v;
      else
        va = V[oval[i]][p] - 48;
      if (b[i][0] == '?')
        vb = v;
      else
        vb = V[ob[i]][p] - 48;
      int now;
      if (op[i][0] == 'A')
        now = (va & vb);
      else if (op[i][0] == 'O')
        now = (va | vb);
      else
        now = (va ^ vb);
      V[i][p] = now + '0';
      ret += now;
    }
  }
  return ret;
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 1; i <= n; ++i) {
      scanf("%s%*s%s", a[i], val[i]);
      if (!isdigit(val[i][0])) {
        scanf("%s%s", op[i], b[i]);
        if (val[i][0] != '?') oval[i] = mop[val[i]];
        if (b[i][0] != '?') ob[i] = mop[b[i]];
      } else {
        oa[i] = i;
        memcpy(V[i], val[i], sizeof(V[i]));
      }
      mop[a[i]] = i;
    }
    for (int i = 0; i < m; ++i) {
      int v1 = check(i, 0);
      int v2 = check(i, 1);
      if (v1 <= v2)
        ans1[i] = '0';
      else
        ans1[i] = '1';
      if (v1 >= v2)
        ans2[i] = '0';
      else
        ans2[i] = '1';
    }
    printf("%s %s\n", ans1, ans2);
  }
  return 0;
}
