#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
const int N = 5010;
int n, m, v1[N], v2[N], num[N][1010], ty[N], ty2[N], val[N], s[30], r1[N],
    r2[N];
map<string, int> name;
char op[N];
int main() {
  scanf("%d%d", &n, &m);
  name[string("?")] = n;
  for (int i = 0; i < n; i++) {
    scanf("%s", op);
    name[op] = i;
    scanf("%s", op);
    scanf("%s", op);
    if (op[0] == '0' || op[0] == '1') {
      ty[i] = 0;
      for (int j = 0; j < m; j++) num[i][j] = op[j] - '0';
    } else {
      ty[i] = 1;
      v1[i] = name[op];
      scanf("%s", op);
      if (op[0] == 'X')
        ty2[i] = 0;
      else if (op[0] == 'A')
        ty2[i] = 1;
      else
        ty2[i] = 2;
      scanf("%s", op);
      v2[i] = name[op];
    }
  }
  for (int j = 0; j < m; j++) {
    for (int oc = 0; oc < 2; oc++) {
      s[oc] = 0;
      val[n] = oc;
      for (int i = 0; i < n; i++) {
        if (ty[i] == 0)
          val[i] = num[i][j];
        else {
          if (ty2[i] == 0)
            val[i] = val[v1[i]] ^ val[v2[i]];
          else if (ty2[i] == 1)
            val[i] = val[v1[i]] & val[v2[i]];
          else
            val[i] = val[v1[i]] | val[v2[i]];
        }
        s[oc] += val[i];
      }
    }
    if (s[0] <= s[1])
      r1[j] = 0;
    else
      r1[j] = 1;
    if (s[0] >= s[1])
      r2[j] = 0;
    else
      r2[j] = 1;
  }
  for (int i = 0; i < m; i++) putchar(r1[i] + '0');
  puts("");
  for (int i = 0; i < m; i++) putchar(r2[i] + '0');
  puts("");
}
