#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<short> coefficients;
  map<string, vector<char>> dict;
  for (int i = 0; i < n; i++) {
    string a, b, c, d, e;
    cin >> a >> b >> c;
    assert(b == string(":="));
    if (c[0] == '0' || c[0] == '1') {
      for (int j = 0; j < c.length(); j++)
        dict[a].push_back((c[j] - '0') * 0b11);
    } else {
      cin >> d >> e;
      for (int j = 0; j < m; j++) {
        int opa = (c[0] == '?') ? 2 : dict[c][j];
        int opb = (e[0] == '?') ? 2 : dict[e][j];
        if (d == string("AND"))
          dict[a].push_back(opa & opb);
        else if (d == string("OR"))
          dict[a].push_back(opa | opb);
        else if (d == string("XOR"))
          dict[a].push_back(opa ^ opb);
        else
          assert(false);
      }
    }
  }
  for (int j = 0; j < m; j++) {
    int sum = 0;
    for (auto &kv : dict) {
      if (kv.second[j] == 2) sum++;
      if (kv.second[j] == 1) sum--;
    }
    coefficients.push_back(sum);
  }
  for (int j = 0; j < m; j++) cout << 1 - (coefficients[j] >= 0);
  cout << "\n";
  for (int j = 0; j < m; j++) cout << (coefficients[j] > 0);
  cout << "\n";
  return 0;
}
