#include <bits/stdc++.h>
using namespace std;
struct Node {
  char op;
  int value;
  vector<int> son;
  bool flag;
  string number;
  Node() {}
  Node(bool flag) : flag(flag) {}
  Node(bool flag, char op) : flag(flag), op(op) {}
  void GetValue(int x, int y) {
    if (op == '&') {
      value = x & y;
      return;
    }
    if (op == '|') {
      value = x | y;
      return;
    }
    if (op == '^') {
      value = x ^ y;
      return;
    }
    exit(1);
  }
};
char GetOp(const string &str) {
  if (str == "AND") {
    return '&';
  }
  if (str == "OR") {
    return '|';
  }
  if (str == "XOR") {
    return '^';
  }
  exit(1);
}
vector<Node> Read(int n) {
  map<string, int> name_index;
  name_index[string("?")] = n;
  vector<Node> graph(n + 1);
  for (int i = 0; i < n; ++i) {
    string name, garbage, s;
    cin >> name >> garbage;
    getline(cin, s);
    reverse(s.begin(), s.end());
    s.pop_back();
    reverse(s.begin(), s.end());
    if (find(s.begin(), s.end(), ' ') == s.end()) {
      graph[i].flag = false;
      graph[i].number = s;
    } else {
      graph[i].flag = true;
      istringstream iss(s);
      string name1, op, name2;
      iss >> name1 >> op >> name2;
      graph[i].son = {name_index[name1], name_index[name2]};
      graph[i].op = GetOp(op);
    }
    name_index[name] = i;
  }
  return graph;
}
pair<string, string> GetResult(vector<Node> &graph, int m) {
  string mx, mn;
  mx.resize(m);
  mn.resize(m);
  for (int i = 0; i < m; ++i) {
    int ans_q[2] = {0, 0};
    for (int &q = graph.back().value = 0; q < 2; ++q) {
      int &ans = ans_q[q];
      for (int j = 0; j < graph.size() - 1; ++j) {
        Node &v = graph[j];
        if (v.flag) {
          v.GetValue(graph[v.son[0]].value, graph[v.son[1]].value);
          ans += v.value;
        } else {
          ans += v.value = v.number[i] - '0';
        }
      }
    }
    if (ans_q[0] == ans_q[1]) {
      mx[i] = mn[i] = '0';
    } else if (ans_q[0] < ans_q[1]) {
      mx[i] = '1';
      mn[i] = '0';
    } else if (ans_q[1] < ans_q[0]) {
      mx[i] = '0';
      mn[i] = '1';
    }
  }
  return {mn, mx};
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<Node> graph = Read(n);
  pair<string, string> ans = GetResult(graph, m);
  cout << ans.first << endl << ans.second << endl;
  return 0;
}
