#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-10;
const long long MOD = 1000000007ll;
const long long mod1 = 1000000009ll;
const long long mod2 = 1100000009ll;
int INF = (int)1e9 + 5;
long long INFINF = (long long)1e18;
int n, m;
map<string, int> enmrate;
pair<int, pair<int, int> > fnc[5010];
string value[5010];
int type[5010];
void parse_input() {
  enmrate["?"] = -1;
  for (int i = 0; i < n; i++) {
    string input, eq, op1, op2, op;
    cin >> input;
    enmrate[input] = i;
    cin >> eq;
    cin >> op1;
    if (op1[0] == '0' || op1[0] == '1') {
      type[i] = 0;
      value[i] = op1;
    } else {
      type[i] = 1;
      cin >> op;
      cin >> op2;
      fnc[i].first = enmrate[op1];
      fnc[i].second.second = enmrate[op2];
      if (op == "AND")
        fnc[i].second.first = 0;
      else if (op == "OR")
        fnc[i].second.first = 1;
      else if (op == "XOR")
        fnc[i].second.first = 2;
    }
  }
}
int get_total(int index_pos, int digit) {
  int ret_total = 0;
  int temp_num[5010];
  for (int i = 0; i < n; i++) {
    if (type[i] == 0) {
      ret_total += (value[i][index_pos] - '0');
      temp_num[i] = (value[i][index_pos] - '0');
    } else {
      int op1, op2, ans;
      if (fnc[i].first == -1)
        op1 = digit;
      else
        op1 = temp_num[fnc[i].first];
      if (fnc[i].second.second == -1)
        op2 = digit;
      else
        op2 = temp_num[fnc[i].second.second];
      if (fnc[i].second.first == 0)
        ans = op1 & op2;
      else if (fnc[i].second.first == 1)
        ans = op1 | op2;
      else
        ans = op1 ^ op2;
      ret_total += ans;
      temp_num[i] = ans;
    }
  }
  return ret_total;
}
int main() {
  scanf("%d %d", &n, &m);
  parse_input();
  vector<int> max_ans(m), min_ans(m);
  for (int i = 0; i < m; i++) {
    int sum_ans[2];
    for (int j = 0; j < 2; j++) {
      sum_ans[j] = get_total(i, j);
    }
    if (sum_ans[0] > sum_ans[1]) {
      max_ans[i] = 0;
      min_ans[i] = 1;
    } else if (sum_ans[1] > sum_ans[0]) {
      max_ans[i] = 1;
      min_ans[i] = 0;
    } else {
      max_ans[i] = 0;
      min_ans[i] = 0;
    }
  }
  for (int i = 0; i < m; i++) printf("%d", min_ans[i]);
  cout << endl;
  for (int i = 0; i < m; i++) printf("%d", max_ans[i]);
  cout << endl;
  return 0;
}
