#include <bits/stdc++.h>
using namespace std;
const int inf = 5002;
vector<pair<pair<int, int>, int> > v;
vector<string> se;
int n, m;
int cnt[inf + 1];
int check(int ind) {
  int res = 0;
  for (int i = 0; i < n; i++) {
    if (v[i].second == -1)
      cnt[i] = se[i][ind] - '0';
    else {
      int fi = v[i].first.first, sse = v[i].first.second;
      if (v[i].second == 0) cnt[i] = (cnt[fi] | cnt[sse]);
      if (v[i].second == 1) cnt[i] = (cnt[fi] ^ cnt[sse]);
      if (v[i].second == 2) cnt[i] = (cnt[fi] & cnt[sse]);
    }
    res += cnt[i];
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  se.resize(n);
  v.resize(n);
  string s;
  getline(cin, s);
  map<string, int> mm;
  mm["?"] = inf;
  for (int i = 0; i < n; i++) {
    getline(cin, s);
    string t;
    int j = 0, sz = (int)s.size();
    while (j < sz && s[j] != ' ') {
      t += s[j];
      j++;
    }
    mm[t] = i;
    j += 4;
    string s1, s2, ss;
    while (j < sz && s[j] != ' ') {
      s1 += s[j];
      j++;
    }
    if (j == sz) {
      v[i].second = -1;
      se[i] = s1;
      continue;
    }
    j++;
    while (s[j] != ' ') {
      ss += s[j];
      j++;
    }
    j++;
    while (j < sz) {
      s2 += s[j];
      j++;
    }
    if (ss == "OR") v[i].second = 0;
    if (ss == "XOR") v[i].second = 1;
    if (ss == "AND") v[i].second = 2;
    v[i].first.first = mm[s1];
    v[i].first.second = mm[s2];
  }
  vector<int> mn(m), mx(m);
  for (int i = 0; i < m; i++) {
    cnt[inf] = 0;
    int c0 = check(i);
    cnt[inf] = 1;
    int c1 = check(i);
    if (c0 >= c1)
      mx[i] = 0;
    else
      mx[i] = 1;
    if (c0 <= c1)
      mn[i] = 0;
    else
      mn[i] = 1;
  }
  for (int i = 0; i < m; i++) cout << mn[i];
  cout << "\n";
  for (int i = 0; i < m; i++) cout << mx[i];
}
