#include <bits/stdc++.h>
using namespace std;
const int N = 5050, M = 1050;
int n, m, c;
unordered_map<string, int> id;
struct var {
  bool t;
  int a, b;
  string op, v;
  var(bool t, string v) : t(t), v(v) {}
  var(bool t, int a, string op, int b) : t(t), a(a), op(op), b(b) {}
};
vector<var> v;
int val[N];
int get(int b, int i) {
  if (i == -1) return c;
  if (val[i] != -1) return val[i];
  if (v[i].t) {
    if (v[i].op == "XOR")
      return val[i] = (get(b, v[i].a) ^ get(b, v[i].b));
    else if (v[i].op == "AND")
      return val[i] = (get(b, v[i].a) & get(b, v[i].b));
    return val[i] = (get(b, v[i].a) | get(b, v[i].b));
  } else
    return val[i] = (v[i].v[b] - '0');
}
string mn, mx;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string s, x;
    cin >> s >> x >> x;
    id[s] = i;
    if (isdigit(x[0]))
      v.push_back(var(0, x));
    else {
      string op, b;
      cin >> op >> b;
      int i1, i2;
      i1 = (x == "?" ? -1 : id[x]);
      i2 = (b == "?" ? -1 : id[b]);
      v.push_back(var(1, i1, op, i2));
    }
  }
  for (int i = 0; i < m; i++) {
    vector<pair<int, int> > p;
    for (int j = 0; j < 2; j++) {
      c = j;
      int cnt[2] = {};
      memset(val, -1, sizeof val);
      for (int k = 0; k < n; k++) cnt[get(i, k)]++;
      p.push_back({cnt[0], cnt[1]});
    }
    if (p[0].first >= p[1].first)
      mn += '0';
    else
      mn += '1';
    if (p[0].second >= p[1].second)
      mx += '0';
    else
      mx += '1';
  }
  cout << mn << '\n' << mx;
}
