#include <bits/stdc++.h>
using namespace std;
struct less_key {
  bool operator()(pair<int64_t, int64_t> p1, pair<int64_t, int64_t> p2) {
    return (p1.first > p2.first) ||
           ((p1.first == p2.first) && (p1.second < p2.second));
  }
};
struct pair_hash {
  std::size_t operator()(const pair<int64_t, int64_t>& k) const {
    return static_cast<size_t>(k.first ^ k.second);
  }
};
const int64_t mod = 100000000007ll;
const int64_t inf = 10000000000000007ll;
int64_t n, m;
struct Value {
  string name;
  int64_t type;
  string con;
  int64_t op;
  string o[2];
};
Value a[5004];
unordered_map<string, int64_t> var;
bool parse(const string& in, const string t, int64_t op, Value& out) {
  size_t q = in.find(t);
  if (q == string::npos) {
    return false;
  }
  out.op = op;
  out.o[0] = in.substr(0, q);
  out.o[1] = in.substr(q + t.size());
  return true;
}
string x;
string xmin;
string xmax;
int64_t check(int64_t m) {
  int64_t r = 0;
  for (int64_t i = 0; i < n; i++) {
    if (!a[i].type) {
      continue;
    }
    int64_t z[2];
    for (int64_t j = 0; j < 2; j++) {
      if (a[i].o[j][0] == '?') {
        z[j] = x[m];
      } else {
        z[j] = a[var[a[i].o[j]]].con[m];
      }
    }
    switch (a[i].op) {
      case 0:
        a[i].con[m] = z[0] ^ z[1];
        break;
      case 1:
        a[i].con[m] = z[0] & z[1];
        break;
      case 2:
        a[i].con[m] = z[0] | z[1];
        break;
    }
    a[i].con[m] += '0';
  }
  for (int64_t i = 0; i < n; i++) {
    r += a[i].con[m] - '0';
  }
  return r;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  string h;
  getline(cin, h);
  for (int64_t i = 0; i < n; i++) {
    string t;
    getline(cin, t);
    const string d = " := ";
    size_t k = t.find(d);
    string v = t.substr(0, k);
    string q = t.substr(k + d.size());
    a[i].name = v;
    if (q[0] == '0' || q[0] == '1') {
      a[i].type = 0;
      a[i].con = q;
    } else {
      a[i].type = 1;
      a[i].con.resize(m);
      parse(q, " XOR ", 0, a[i]);
      parse(q, " AND ", 1, a[i]);
      parse(q, " OR ", 2, a[i]);
    }
  }
  for (int64_t i = 0; i < n; i++) {
    var[a[i].name] = i;
  }
  x.resize(m);
  xmin.resize(m);
  xmax.resize(m);
  for (int64_t i = 0; i < m; i++) {
    x[i] = '0';
    int64_t v0 = check(i);
    x[i] = '1';
    int64_t v1 = check(i);
    if (v0 == v1) {
      xmin[i] = '0';
      xmax[i] = '0';
    } else {
      if (v0 < v1) {
        xmin[i] = '0';
        xmax[i] = '1';
      } else {
        xmin[i] = '1';
        xmax[i] = '0';
      }
    }
  }
  cout << xmin << endl;
  cout << xmax << endl;
  return 0;
}
