#include <bits/stdc++.h>
using namespace std;
string bits[5555];
vector<int> edges[5555];
string operations[5555];
int GetVariableId(const string& S, map<string, int>* variables, int n) {
  if (S == "?") return n;
  if (variables->find(S) == variables->end()) {
    int sz = variables->size();
    (*variables)[S] = sz;
  }
  return (*variables)[S];
}
void GetVariables(const string& S, map<string, int>* variables, int n) {
  std::istringstream iss(S);
  vector<string> tmp;
  while (iss) {
    std::string sub;
    iss >> sub;
    if (sub.empty()) break;
    tmp.push_back(sub);
  }
  int id1 = GetVariableId(tmp[0], variables, n);
  if (tmp.size() == 3) {
    bits[id1] = tmp[2];
  } else if (tmp.size() == 5) {
    edges[id1].push_back(GetVariableId(tmp[2], variables, n));
    edges[id1].push_back(GetVariableId(tmp[4], variables, n));
    operations[id1] = tmp[3];
  } else {
    exit(-1);
  }
}
int val[5555];
int dfs(int x, int pos) {
  if (val[x] != -1) return val[x];
  if (edges[x].empty()) {
    return val[x] = bits[x][pos] - '0';
  }
  int b1 = dfs(edges[x][0], pos);
  int b2 = dfs(edges[x][1], pos);
  if (operations[x] == "XOR") return val[x] = (b1 ^ b2);
  if (operations[x] == "AND") return val[x] = (b1 & b2);
  if (operations[x] == "OR") return val[x] = (b1 | b2);
  return -1;
}
int main() {
  int n, m;
  cin >> n >> m;
  string S;
  getline(cin, S);
  map<string, int> variables;
  for (int i = 1; i <= n; i++) {
    getline(cin, S);
    GetVariables(S, &variables, n);
  }
  bits[n].clear();
  for (int i = 0; i < m; i++) bits[n].push_back('0');
  string ans1, ans2;
  for (int j = 0; j < m; j++) {
    int minn_summ = 100000000;
    int minn_choice = -1;
    int maxx_summ = -1;
    int maxx_choice = -1;
    for (int choice = 0; choice <= 1; choice++) {
      bits[n][j] = choice + '0';
      for (int i = 0; i <= n; i++) {
        val[i] = -1;
      }
      for (int i = 0; i < n; i++) {
        if (val[i] == -1) {
          dfs(i, j);
        }
      }
      int summ = 0;
      for (int i = 0; i < n; i++) {
        summ += val[i];
      }
      if (summ < minn_summ) {
        minn_summ = summ;
        minn_choice = choice;
      }
      if (summ > maxx_summ) {
        maxx_summ = summ;
        maxx_choice = choice;
      }
    }
    ans1.push_back(minn_choice + '0');
    ans2.push_back(maxx_choice + '0');
  }
  cout << ans1 << endl;
  cout << ans2 << endl;
}
