#include <bits/stdc++.h>
using namespace std;
int ans[5001][1000][2];
int main() {
  cin.tie(0), cerr.tie(0), ios_base::sync_with_stdio(0);
  long long int n, m;
  cin >> n >> m;
  map<string, int> mp;
  for (int j = 1; j <= n; j++) {
    string z, x, y, temp, op;
    cin >> z;
    mp[z] = j;
    cin >> temp;
    cin >> x;
    if (x[0] == '1' or x[0] == '0') {
      for (int i = 0; i < m; ++i) {
        ans[j][i][0] = x[i] - '0';
        ans[j][i][1] = x[i] - '0';
      }
    } else {
      cin >> op;
      cin >> y;
      for (int i = 0; i < 2; i++) {
        for (int u = 0; u < m; u++) {
          int a, b;
          if (x == "?") {
            a = i;
          } else {
            a = ans[mp[x]][u][i];
          }
          if (y == "?") {
            b = i;
          } else {
            b = ans[mp[y]][u][i];
          }
          if (op == "XOR") {
            ans[j][u][i] = a ^ b;
          } else if (op == "AND") {
            ans[j][u][i] = a & b;
          } else {
            ans[j][u][i] = a | b;
          }
        }
      }
    }
  }
  int finalm[m];
  int finalma[m];
  for (int i = 0; i < m; i++) {
    long long int count0 = 0, count1 = 0;
    for (int u = 0; u < 2; u++) {
      for (int v = 1; v <= n; v++) {
        if (u == 0) {
          count0 += ans[v][i][u];
        } else {
          count1 += ans[v][i][u];
        }
      }
    }
    if (count1 > count0) {
      finalm[i] = 0;
      finalma[i] = 1;
    } else if (count1 == count0) {
      finalm[i] = 0;
      finalma[i] = 0;
    } else {
      finalm[i] = 1;
      finalma[i] = 0;
    }
  }
  for (int i = 0; i < m; i++) {
    cout << finalm[i];
  }
  cout << endl;
  for (int i = 0; i < m; i++) {
    cout << finalma[i];
  }
  return 0;
}
