#include <bits/stdc++.h>
using namespace std;
map<string, int> id;
map<string, int> opp;
struct node {
  int id1, id2;
  int op;
  int in;
  int tt;
  string v;
} k[6000];
vector<int> to[6000];
vector<int> ids;
int ans1[2000];
int ans2[2000];
void topp(int i) {
  if (i != 0) ids.push_back(i);
  for (int j = 0; j < to[i].size(); j++) {
    k[to[i][j]].in--;
    if (k[to[i][j]].in == 0) topp(to[i][j]);
  }
}
int main() {
  int n, m;
  ios::sync_with_stdio(false);
  cin >> n >> m;
  string s1, ss, s2, so, s3;
  id["?"] = 0;
  k[0].in = 1;
  opp["AND"] = 1;
  opp["OR"] = 2;
  opp["XOP"] = 3;
  string sp;
  for (int i = 1; i <= m; i++) {
    sp += " ";
  }
  k[0].v = sp;
  for (int i = 1; i <= n; i++) {
    cin >> s1 >> ss >> s2;
    id[s1] = i;
    k[i].in = 0;
    if (s2[0] != '0' && s2[0] != '1') {
      cin >> so >> s3;
      k[i].tt = 0;
      k[i].v = sp;
      k[i].op = opp[so];
      k[i].id1 = id[s2];
      k[i].id2 = id[s3];
      if (k[id[s2]].in != 0) {
        k[i].in++;
        to[id[s2]].push_back(i);
      }
      if (k[id[s3]].in != 0) {
        k[i].in++;
        to[id[s3]].push_back(i);
      }
    } else {
      k[i].v = s2;
      k[i].tt = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (k[i].in == 0) ids.push_back(i);
  }
  topp(0);
  int ans[2];
  int tans;
  for (int i = 0; i < m; i++) {
    for (int kk = 0; kk <= 1; kk++) {
      k[0].v[i] = '0' + kk;
      ans[kk] = 0;
      for (int j = 0; j < ids.size(); j++) {
        if (k[ids[j]].tt == 1) {
          ans[kk] += (k[ids[j]].v[i] - '0');
          continue;
        }
        int v1 = k[k[ids[j]].id1].v[i] - '0';
        int v2 = k[k[ids[j]].id2].v[i] - '0';
        if (k[ids[j]].op == 1) {
          tans = v1 & v2;
        } else if (k[ids[j]].op == 2) {
          tans = v1 | v2;
        } else {
          tans = v1 ^ v2;
        }
        ans[kk] += tans;
        k[ids[j]].v[i] = tans + '0';
      }
    }
    if (ans[0] > ans[1]) {
      ans1[i] = 1;
      ans2[i] = 0;
    } else if (ans[1] > ans[0]) {
      ans1[i] = 0;
      ans2[i] = 1;
    } else
      ans1[i] = ans2[i] = 0;
  }
  for (int i = 0; i < m; i++) cout << ans1[i];
  cout << endl;
  for (int i = 0; i < m; i++) cout << ans2[i];
  cout << endl;
}
