#include <bits/stdc++.h>
using namespace std;
const long long oo = 1e18;
const int N = 5e3 + 1;
int n, m, it, val[N][1000][2], cnt[1000][2][2];
string s, t;
void process() {
  t.clear();
  while (s[it] == ' ') it++;
  while (it < s.size() && s[it] != ' ') {
    t += s[it];
    it++;
  }
}
int compute(int x, int y, int tmp) {
  if (tmp == 0) return x & y;
  if (tmp == 1) return x | y;
  if (tmp == 2) return x ^ y;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  int test = 1;
  while (test--) {
    getline(cin, s);
    it = 0;
    process();
    n = stoi(t);
    process();
    m = stoi(t);
    map<string, int> ID;
    ID["?"] = 0;
    for (int i = 0; i < m; i++) {
      val[0][i][0] = 0;
      val[0][i][1] = 1;
    }
    for (int i = 1; i <= n; i++) {
      getline(cin, s);
      it = 0;
      process();
      ID[t] = i;
      it += 4;
      if (s[it] == '0' || s[it] == '1') {
        for (int j = 0; j < m; j++) {
          int t1 = s[it + j] - '0';
          val[i][j][0] = t1;
          val[i][j][1] = t1;
          cnt[j][0][t1]++;
          cnt[j][1][t1]++;
        }
      } else {
        int i1, i2, op;
        process();
        i1 = ID[t];
        process();
        if (t == "AND")
          op = 0;
        else if (t == "OR")
          op = 1;
        else if (t == "XOR")
          op = 2;
        process();
        i2 = ID[t];
        for (int j = 0; j < m; j++) {
          val[i][j][0] = compute(val[i1][j][0], val[i2][j][0], op);
          val[i][j][1] = compute(val[i1][j][1], val[i2][j][1], op);
          cnt[j][0][val[i][j][0]]++;
          cnt[j][1][val[i][j][1]]++;
        }
      }
    }
    string ans[2];
    for (int i = 0; i < m; i++) {
      if (cnt[i][0][0] == cnt[i][1][0]) {
        ans[0] += '0';
        ans[1] += '0';
      } else {
        if (cnt[i][0][0] > cnt[i][1][0]) {
          ans[0] += '0';
          ans[1] += '1';
        } else {
          ans[0] += '1';
          ans[1] += '0';
        }
      }
    }
    cout << ans[0] << endl;
    cout << ans[1] << endl;
  }
}
