#include <bits/stdc++.h>
using namespace std;
int n, m;
string a, b, c, d, e;
vector<vector<int> > vals;
vector<vector<int> > forms(1);
map<string, int> getind;
int doop(int left, int right, int op) {
  if (op == 2) return left ^ right;
  if (op == 3)
    return left & right;
  else
    return left | right;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  vals.resize(1, vector<int>(m, 0));
  getind["?"] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    getind[a] = vals.size();
    vals.resize(vals.size() + 1);
    forms.resize(forms.size() + 1);
    if (c[0] != '1' && c[0] != '0') {
      cin >> d >> e;
      int first = getind[c];
      int sec = getind[e];
      int op = 2;
      if (d == "AND")
        op = 3;
      else if (d == "OR")
        op = 4;
      forms[forms.size() - 1].push_back(first);
      forms[forms.size() - 1].push_back(op);
      forms[forms.size() - 1].push_back(sec);
      for (int j = 0; j < m; j++) {
        vals[vals.size() - 1].push_back(doop(vals[first][j], vals[sec][j], op));
      }
    } else {
      for (int j = 0; j < m; j++) {
        if (c[j] == '1')
          vals[vals.size() - 1].push_back(1);
        else
          vals[vals.size() - 1].push_back(0);
      }
    }
  }
  vector<int> best(m, 0);
  vector<int> worst(m, 0);
  for (int i = 0; i < m; i++) {
    int curcnt = 0;
    int rescnt = 0;
    for (int j = 1; j <= n; j++) {
      if (forms[j].size() > 0) {
        curcnt += vals[j][i];
      }
    }
    vals[0][i] = 1;
    for (int j = 1; j <= n; j++) {
      if (forms[j].size() > 0) {
        int first = forms[j][0];
        int sec = forms[j][2];
        int op = forms[j][1];
        int res = doop(vals[first][i], vals[sec][i], op);
        vals[j][i] = res;
        rescnt += res;
      }
    }
    if (rescnt > curcnt) best[i] = 1;
    if (rescnt < curcnt) worst[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    cout << worst[i];
  }
  cout << "\n";
  for (int i = 0; i < m; i++) {
    cout << best[i];
  }
  return 0;
}
