#include <bits/stdc++.h>
using namespace std;
int n, m, val[5010][1010], f[5010], ans[2][1010];
string _s, a[5010], b[5010];
map<string, int> name;
int Get(string ss) { return name[ss]; }
int _check(int x, int y) {
  int Sum = 0, k = 1, g[5010];
  for (int i = 1; i <= n; i++) {
    if (f[i] == 0)
      Sum += g[i] = val[i][x];
    else {
      int k1, k2;
      if (a[i] == "?")
        k1 = y;
      else
        k1 = g[Get(a[i])];
      if (b[i] == "?")
        k2 = y;
      else
        k2 = g[Get(b[i])];
      if (f[i] == 1) Sum += g[i] = k1 & k2;
      if (f[i] == 2) Sum += g[i] = k1 | k2;
      if (f[i] == 3) Sum += g[i] = k1 ^ k2;
    }
  }
  return Sum;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    cin >> _s;
    name[_s] = i;
    cin >> _s >> _s;
    if (_s[0] == '0' || _s[0] == '1')
      for (int j = 1; j <= m; j++) val[i][j] = _s[j - 1] - '0';
    else {
      a[i] = _s;
      cin >> _s;
      if (_s == "AND")
        f[i] = 1;
      else if (_s == "OR")
        f[i] = 2;
      else
        f[i] = 3;
      cin >> _s;
      b[i] = _s;
    }
  }
  for (int i = 1; i <= m; i++) {
    int q1, q2;
    q1 = _check(i, 1);
    q2 = _check(i, 0);
    if (q1 < q2)
      ans[0][i] = 1, ans[1][i] = 0;
    else if (q1 > q2)
      ans[0][i] = 0, ans[1][i] = 1;
    else
      ans[0][i] = ans[1][i] = 0;
  }
  for (int p = 0; p <= 1; p++) {
    for (int i = 1; i <= m; i++) printf("%d", ans[p][i]);
    printf("\n");
  }
}
