#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long double EPS = 1e-7;
long long oper(char c1, char c2, string t) {
  if (t == "AND") {
    if (c1 == '1' && c2 == '1') {
      return 1;
    }
    return 0;
  }
  if (t == "OR") {
    if (c1 == '1' || c2 == '1') {
      return 1;
    }
    return 0;
  }
  if (t == "XOR") {
    if ((c1 == '0' && c2 == '1') || (c1 == '1' && c2 == '0')) {
      return 1;
    }
    return 0;
  }
  return -1;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  map<string, string> var;
  vector<long long> res0(m, 0), res1(m, 0);
  map<string, pair<string, string> > vals;
  for (long long i = 0; i < n; ++i) {
    string name, name1, name2;
    cin >> name >> name1 >> name1;
    if (name1[0] == '0' || name1[0] == '1') {
      vals[name] = {name1, name1};
    } else {
      string t;
      cin >> t >> name2;
      for (long long j = 0; j < m; ++j) {
        long long v1 = oper(((name1 == "?") ? '0' : vals[name1].first[j]),
                            ((name2 == "?") ? '0' : vals[name2].first[j]), t);
        long long v2 = oper(((name1 == "?") ? '1' : vals[name1].second[j]),
                            ((name2 == "?") ? '1' : vals[name2].second[j]), t);
        vals[name].first.push_back('0' + v1);
        vals[name].second.push_back('0' + v2);
        res0[j] += v1;
        res1[j] += v2;
      }
    }
  }
  string ans1, ans2;
  for (long long i = 0; i < m; ++i) {
    if (res0[i] == res1[i]) {
      ans1.push_back('0');
      ans2.push_back('0');
    } else if (res0[i] > res1[i]) {
      ans2.push_back('0');
      ans1.push_back('1');
    } else {
      ans2.push_back('1');
      ans1.push_back('0');
    }
  }
  cout << ans1 << endl;
  cout << ans2 << endl;
  return 0;
}
