#include <bits/stdc++.h>
using namespace std;
const int xor_val = (int)'X';
const int and_val = (int)'A';
const int or_val = (int)'O';
void get_ith_bit(vector<vector<vector<int> > >& var_info, int ith, int& n,
                 vector<pair<int, int> >& ans) {
  vector<unordered_set<int> > var_num(2);
  for (int i = 0; i < n; i++) {
    switch (var_info[i][ith][0]) {
      case -2:
        break;
      case -1: {
        var_num[0].insert(i);
        var_num[1].insert(i);
        break;
      }
      default: {
        bool ans[2] = {false, true};
        for (int k = 0; k < 2; k++) {
          bool v1 =
              (var_info[i][ith][0] == n)
                  ? ans[k]
                  : (var_num[k].find(var_info[i][ith][0]) != var_num[k].end());
          bool v2 =
              (var_info[i][ith][2] == n)
                  ? ans[k]
                  : (var_num[k].find(var_info[i][ith][2]) != var_num[k].end());
          switch (var_info[i][ith][1]) {
            case xor_val: {
              ans[k] = v1 ^ v2;
              break;
            }
            case or_val: {
              ans[k] = v1 || v2;
              break;
            }
            case and_val:
              ans[k] = v1 && v2;
          }
        }
        if (ans[0]) var_num[0].insert(i);
        if (ans[1]) var_num[1].insert(i);
      }
    }
  }
  ans[ith].first = (var_num[0].size() > var_num[1].size());
  ans[ith].second = (var_num[0].size() < var_num[1].size());
}
int main(int argc, char const* argv[]) {
  int n, m;
  cin >> n >> m;
  unordered_map<string, int> var_index;
  vector<vector<vector<int> > > var_info(
      n, vector<vector<int> >(m, vector<int>(3, 0)));
  string varname;
  string s;
  string v1, op, v2;
  for (int i = 0; i < n; i++) {
    cin >> varname;
    cin >> s;
    cin >> v1;
    var_index[varname] = i;
    if (v1[0] == '0' || v1[0] == '1')
      for (int j = 0; j < m; j++) var_info[i][j][0] = (int)(v1[j] - '0') - 2;
    else {
      cin >> op;
      cin >> v2;
      for (int j = 0; j < m; j++) {
        var_info[i][j][0] = (v1[0] == '?') ? n : var_index[v1];
        var_info[i][j][1] = (int)(op[0]);
        var_info[i][j][2] = (v2[0] == '?') ? n : var_index[v2];
      }
    }
  }
  vector<pair<int, int> > min_max(m);
  for (int i = 0; i < m; i++) get_ith_bit(var_info, i, n, min_max);
  for (int i = 0; i < m; i++) cout << min_max[i].first;
  cout << "\n";
  for (int i = 0; i < m; i++) cout << min_max[i].second;
  return 0;
}
