#include <bits/stdc++.h>
using namespace std;
long long INV2 = 500000004;
long long INV6 = 166666668;
long long power(long long a, long long b, long long c) {
  long long x = 1, y = a;
  while (b > 0) {
    if (b & 1) x = (x * y) % c;
    y = (y * y) % c;
    b /= 2;
  }
  return x % c;
}
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
int dr[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dc[] = {0, 1, 1, 1, 0, -1, -1, -1};
string s1, s2, s3, s4, tmp;
int dp[2][1005][5005], ans[2][1005];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  map<string, int> mk;
  for (int i = 0; i < n; ++i) {
    cin >> s1 >> tmp;
    mk[s1] = i;
    cin >> s2;
    if (s2[0] == '1' || s2[0] == '0') {
      for (int j = 0; j < m; ++j) {
        dp[0][j][i] = dp[1][j][i] = s2[j] - '0';
      }
    } else {
      cin >> s3 >> s4;
      int f, s;
      for (int j = 0; j < m; ++j) {
        if (s2[0] == '?') {
          f = 0;
        } else {
          f = dp[0][j][mk[s2]];
        }
        if (s4[0] == '?') {
          s = 0;
        } else {
          s = dp[0][j][mk[s4]];
        }
        if (s3[0] == 'X')
          dp[0][j][i] = f ^ s;
        else if (s3[0] == 'A')
          dp[0][j][i] = f & s;
        else
          dp[0][j][i] = f | s;
        if (s2[0] == '?') {
          f = 1;
        } else {
          f = dp[1][j][mk[s2]];
        }
        if (s4[0] == '?') {
          s = 1;
        } else {
          s = dp[1][j][mk[s4]];
        }
        if (s3[0] == 'X')
          dp[1][j][i] = f ^ s;
        else if (s3[0] == 'A')
          dp[1][j][i] = f & s;
        else
          dp[1][j][i] = f | s;
      }
    }
  }
  for (int j = 0; j < m; ++j) {
    int c0 = 0, c1 = 0;
    for (int i = 0; i < n; ++i) {
      if (dp[0][j][i]) c0++;
      if (dp[1][j][i]) c1++;
    }
    if (c0 <= c1) {
      ans[0][j] = 0;
    } else {
      ans[0][j] = 1;
    }
    if (c0 >= c1) {
      ans[1][j] = 0;
    } else {
      ans[1][j] = 1;
    }
  }
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < m; ++j) {
      printf("%d", ans[i][j]);
    }
    printf("\n");
  }
  return 0;
}
