#include <bits/stdc++.h>
using namespace std;
struct value {
  bitset<1000> bits;
  virtual void read(map<string, value *> &) = 0;
  virtual int cal() = 0;
};
struct cons : public value {
  void read(map<string, value *> &v) { cin >> bits; }
  int rev;
  cons() : rev(1) {}
  int cal() { return rev; }
};
struct expr : public value {
  value **a;
  value **b;
  enum { AND, OR, XOR } op;
  void read(map<string, value *> &v) {
    string str;
    cin >> str;
    a = &v[str];
    cin >> str;
    if (str == "AND")
      op = AND;
    else if (str == "OR")
      op = OR;
    else if (str == "XOR")
      op = XOR;
    cin >> str;
    b = &v[str];
  }
  int rev;
  int ra;
  int rb;
  int cal() {
    int ca = (*a)->cal();
    int cb = (*b)->cal();
    if (ca == ra && cb == rb) return rev;
    ra = ca;
    rb = cb;
    bits = (*a)->bits;
    switch (op) {
      case AND:
        bits &= (*b)->bits;
        break;
      case OR:
        bits |= (*b)->bits;
        break;
      case XOR:
        bits ^= (*b)->bits;
        break;
    }
    return ++rev;
  }
};
void sum(short r[1000], map<string, value *> &val) {
  for (int i = 0; i < 1000; i++) r[i] = 0;
  for (map<string, value *>::iterator it = val.begin(); it != val.end(); it++) {
    if (it->first == "?") continue;
    it->second->cal();
    for (int i = 0; i < 1000; i++) {
      r[i] += it->second->bits[i] ? +1 : -1;
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  map<string, value *> val;
  for (int i = 0; i < n; i++) {
    string name;
    cin >> name;
    cin.ignore(4);
    switch (cin.peek()) {
      case '0':
      case '1':
        val[name] = new cons;
        break;
      default:
        val[name] = new expr;
    }
    val[name]->read(val);
  }
  cons p;
  val["?"] = &p;
  short z[1000], o[1000];
  sum(z, val);
  p.bits.set();
  p.rev++;
  sum(o, val);
  for (int i = m - 1; i >= 0; i--) cout << (o[i] < z[i]);
  cout << endl;
  for (int i = m - 1; i >= 0; i--) cout << (o[i] > z[i]);
  cout << endl;
}
