#include <bits/stdc++.h>
using namespace std;
vector<int> oper1, oper2;
vector<string> oper;
vector<int> v;
vector<string> vec(5001);
vector<int> vec1(5001);
map<string, int> mp;
int calc(int zeroOne, int i) {
  int res = 0;
  int t = oper.size();
  int a, b, xx;
  for (int j = 0; j < t; j++) {
    if (oper1[j] == -1)
      a = zeroOne;
    else {
      int x = oper1[j];
      if (vec[x] != "-")
        a = vec[x][i] - '0';
      else
        a = vec1[x];
    }
    if (oper2[j] == -1)
      b = zeroOne;
    else {
      int x = oper2[j];
      if (vec[x] != "-")
        b = vec[x][i] - '0';
      else
        b = vec1[x];
    }
    if (oper[j] == "XOR")
      xx = a ^ b;
    else if (oper[j] == "OR")
      xx = a | b;
    else
      xx = a & b;
    res += xx;
    vec1[v[j]] = xx;
  }
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  string var, sign, s, s1, s2;
  for (int i = 0; i < 5001; i++) vec[i] = "-";
  for (int i = 0; i < n; i++) {
    cin >> var >> sign;
    mp[var] = i;
    cin >> s;
    if (s[0] == '0' || s[0] == '1') {
      vec[i] = s;
      continue;
    }
    v.push_back(i);
    if (s == "?")
      oper1.push_back(-1);
    else
      oper1.push_back(mp[s]);
    cin >> s1;
    oper.push_back(s1);
    cin >> s2;
    if (s2 == "?")
      oper2.push_back(-1);
    else
      oper2.push_back(mp[s2]);
  }
  string ans1, ans2;
  for (int i = 0; i < m; i++) {
    int res0 = calc(0, i);
    int res1 = calc(1, i);
    if (res0 >= res1)
      ans1.push_back('0');
    else
      ans1.push_back('1');
    if (res0 <= res1)
      ans2.push_back('0');
    else
      ans2.push_back('1');
  }
  cout << ans2 << endl;
  cout << ans1 << endl;
  return 0;
}
