#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
map<string, pair<vector<int>, vector<int> > > mp;
int band(int a, int b) { return a & b; }
int bor(int a, int b) { return a | b; }
int bxor(int a, int b) { return a ^ b; }
int n, m, (*oper)(int, int);
int tot0[N], tot1[N];
int main() {
  int i;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    string cur, nop;
    cin >> cur >> nop;
    char op[N * 2], c1[N], c2[N], c3[N];
    scanf("%[^\n]", op);
    if (sscanf(op, "%s%s%s", c1, c2, c3) == 1) {
      vector<int> t;
      t.clear(), t.resize(m);
      for (int k = m - 1; k >= 0; k--)
        t[k] = c1[m - k - 1] - '0', tot0[k] += t[k], tot1[k] += t[k];
      mp[cur] = make_pair(t, t);
    } else {
      string s1 = string(c1), s3 = string(c3);
      vector<int> t0, t1;
      t0.clear(), t1.clear();
      t0.resize(m), t1.resize(m);
      vector<int> v10, v30, v11, v31;
      if (c1[0] != '?') v10 = mp[s1].first, v11 = mp[s1].second;
      if (c3[0] != '?') v30 = mp[s3].first, v31 = mp[s3].second;
      if (c2[0] == 'A')
        oper = &band;
      else if (c2[0] == 'O')
        oper = &bor;
      else
        oper = &bxor;
      for (int k = m - 1; k >= 0; k--) {
        t0[k] = oper((c1[0] == '?' ? 0 : v10[k]), (c3[0] == '?' ? 0 : v30[k]));
        t1[k] = oper((c1[0] == '?' ? 1 : v11[k]), (c3[0] == '?' ? 1 : v31[k]));
        tot0[k] += t0[k];
        tot1[k] += t1[k];
      }
      mp[cur] = make_pair(t0, t1);
    }
  }
  for (int k = m - 1; k >= 0; k--) putchar(tot0[k] <= tot1[k] ? '0' : '1');
  puts("");
  for (int k = m - 1; k >= 0; k--) putchar(tot0[k] >= tot1[k] ? '0' : '1');
  puts("");
  return 0;
}
