#include <bits/stdc++.h>
using namespace std;
map<string, int> mapp;
string tep[5005];
int t[5005][1050];
int vis[5005][2];
int step[5005];
int ans[2][5005];
int n, m;
int dfs(int index, int pos, int v) {
  if (index == -1) {
    return v;
  }
  if (step[index] == 0) {
    t[index][pos] = tep[index][pos] - '0';
  }
  if (t[index][pos] != -1) {
    return t[index][pos];
  }
  int a = dfs(vis[index][0], pos, v);
  int b = dfs(vis[index][1], pos, v);
  int ans;
  if (step[index] == 1) {
    ans = a | b;
  } else if (step[index] == 2) {
    ans = a ^ b;
  } else if (step[index] == 3) {
    ans = a & b;
  }
  t[index][pos] = ans;
  return ans;
}
int get(int pos, int v) {
  int ans = 0;
  memset(t, -1, sizeof(t));
  for (int i = 0; i < n; i++) {
    ans += dfs(i, pos, v);
  }
  return ans;
}
int main() {
  mapp["?"] = -1;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    mapp[str] = i;
    cin >> str;
    cin >> str;
    if (str[0] == '1' || str[0] == '0') {
      tep[i] = str;
    } else {
      vis[i][0] = mapp[str];
      cin >> str;
      if (str[0] == 'O') {
        step[i] = 1;
      } else if (str[0] == 'X') {
        step[i] = 2;
      } else {
        step[i] = 3;
      }
      cin >> str;
      vis[i][1] = mapp[str];
    }
  }
  for (int i = 0; i < m; i++) {
    int a = get(i, 0), b = get(i, 1);
    if (a == b) {
      ans[0][i] = ans[1][i] = 0;
    } else if (a < b) {
      ans[1][i] = 1;
      ans[0][i] = 0;
    } else {
      ans[1][i] = 0;
      ans[0][i] = 1;
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < m; j++) {
      printf("%d", ans[i][j]);
    }
    printf("\n");
  }
}
