#include <bits/stdc++.h>
const int N = 1e5 + 10;
using namespace std;
pair<string, string> a[N];
int b[N], n, m, c[N], cnt = 0;
map<string, int> f;
pair<int, int> d[N];
vector<string> split(string s) {
  vector<string> ansf;
  int it = 0;
  int len = s.length();
  while (it < len) {
    if (s[it] == ' ') {
      it++;
      continue;
    }
    int jt = it;
    while (jt < len - 1 && s[jt + 1] != ' ') jt++;
    ansf.push_back(s.substr(it, jt - it + 1));
    it = jt + 1;
  }
  return ansf;
}
void init() {
  for (int i = 1; i <= cnt; i++) {
    if (a[i].first == "?") {
      d[i].first = -1;
    } else {
      d[i].first = f[a[i].first];
    }
    if (a[i].second == "?") {
      d[i].second = -1;
    } else {
      d[i].second = f[a[i].second];
    }
  }
}
int test(bool x, int pos) {
  int ret = 0;
  for (int i = 1; i <= cnt; i++) {
    bool x1, x2, x3;
    if (d[i].first == -1) {
      x1 = x;
    } else if (d[i].first > 0) {
      x1 = c[d[i].first];
    } else {
      x1 = a[i].first[pos] - '0';
    }
    if (d[i].second == -1) {
      x2 = x;
    } else if (d[i].second > 0) {
      x2 = c[d[i].second];
    } else {
      x2 = a[i].second[pos] - '0';
    }
    if (b[i] == 0) {
      x3 = x1 & x2;
    } else if (b[i] == 1) {
      x3 = x1 ^ x2;
    } else {
      x3 = x1 | x2;
    }
    if (x3) {
      ret++;
    }
    c[i] = x3;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  string tmp;
  for (int i = 0; i < m; i++) {
    tmp += "1";
  }
  string s;
  getline(cin, s);
  for (int i = 0; i < n; i++) {
    getline(cin, s);
    vector<string> t = split(s);
    f[t[0]] = ++cnt;
    if (t.size() <= 3) {
      a[cnt].first = t[2];
      a[cnt].second = tmp;
      b[cnt] = 0;
    } else {
      a[cnt].first = t[2];
      a[cnt].second = t[4];
      if (t[3] == "XOR") {
        b[cnt] = 1;
      } else if (t[3] == "AND") {
        b[cnt] = 0;
      } else {
        b[cnt] = 2;
      }
    }
  }
  init();
  string ans1, ans2;
  for (int i = 0; i < m; i++) {
    int x1 = test(0, i), x2 = test(1, i);
    if (x1 > x2) {
      ans1 += "1";
      ans2 += "0";
    } else if (x1 < x2) {
      ans1 += "0";
      ans2 += "1";
    } else {
      ans1 += "0";
      ans2 += "0";
    }
  }
  cout << ans1 << endl << ans2;
  return 0;
}
