#include <bits/stdc++.h>
using namespace std;
const int N = 5010, M = 1010;
map<string, int> id;
int l = 0;
char s[1010], s1[N][M], s2[N][M], op[N][20];
int a[N], b[N];
char a1[M], a2[M];
int val[N];
int calc(char *s, int j, int d, int a) {
  if (s[0] == '?')
    return d;
  else if ('a' <= s[0] && s[0] <= 'z')
    return val[a];
  else
    return s[j] - '0';
}
int n, m;
int get(int j, int d) {
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    int A = calc(s1[i], j, d, a[i]);
    if (!op[i][0])
      val[i] = A;
    else {
      int B = calc(s2[i], j, d, b[i]);
      if (op[i][0] == 'O')
        val[i] = A | B;
      else if (op[i][0] == 'X')
        val[i] = A ^ B;
      else if (op[i][0] == 'A')
        val[i] = A & B;
    }
    sum += val[i];
  }
  return sum;
}
int main() {
  scanf("%d%d", &n, &m);
  scanf("%s", s);
  for (int i = 1; i <= n; i++) {
    id[s] = i;
    scanf("%s", s);
    scanf("%s", s1[i]);
    if ('a' <= s1[i][0] && s1[i][0] <= 'z') a[i] = id[s1[i]];
    scanf("%s", s);
    if ('A' <= s[0] && s[0] <= 'Z') {
      op[i][0] = s[0];
      scanf("%s", s2[i]);
      if ('a' <= s2[i][0] && s2[i][0] <= 'z') b[i] = id[s2[i]];
      if (i < n) scanf("%s", s);
    } else
      op[i][0] = 0;
  }
  for (int i = 0; i < m; i++) {
    int t1 = get(i, 0), t2 = get(i, 1);
    if (t1 == t2)
      a1[i] = a2[i] = '0';
    else if (t1 < t2)
      a1[i] = '0', a2[i] = '1';
    else
      a1[i] = '1', a2[i] = '0';
  }
  printf("%s\n", a1);
  printf("%s\n", a2);
  return 0;
}
