#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int inf = numeric_limits<int>::max() / 2;
const long double eps = 1e-9;
const long double pi = acos(-1);
template <typename T>
inline bool mineq(T& a, T b) {
  return (a > b) ? (a = b, 1) : 0;
}
template <typename T>
inline bool maxeq(T& a, T b) {
  return (a < b) ? (a = b, 1) : 0;
}
inline void solve(), read();
const string file = "";
signed main() {
  if (file != "") {
    freopen((file + ".in").c_str(), "r", stdin);
    freopen((file + ".out").c_str(), "w", stdout);
  }
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  read();
  solve();
  return 0;
}
struct vertex {
  string name;
  string op;
  string a, b;
};
char $xor(char a, char b) { return '0' + (a != b); }
char $and(char a, char b) {
  if (a == '1' && b == '1') {
    return '1';
  } else {
    return '0';
  }
}
char $or(char a, char b) {
  if (a == '1' || b == '1') {
    return '1';
  } else {
    return '0';
  }
}
int isnum(string& s) { return isdigit(s[0]); }
int n, m;
vector<string> ss;
vector<vertex> vs;
unordered_map<string, char> ms;
void parse(string& s, vertex& v) {
  int j = 0;
  while (j < (int)((s).size()) && s[j] != ' ') {
    v.name += s[j];
    ++j;
  }
  j += 4;
  while (j < (int)((s).size()) && s[j] != ' ') {
    v.a += s[j];
    ++j;
  }
  if (j == (int)((s).size())) {
    v.op = "VAR";
    return;
  }
  ++j;
  while (j < (int)((s).size()) && s[j] != ' ') {
    v.op += s[j];
    ++j;
  }
  ++j;
  while (j < (int)((s).size()) && s[j] != ' ') {
    v.b += s[j];
    ++j;
  }
}
char rec(string& s, int i, int j) {
  if (isnum(s)) {
    return s[j];
  }
  if (ms.count(s)) {
    return ms[s];
  }
  char a = rec(vs[i].a, i, j);
  if (vs[i].op == "VAR") {
    return ms[s] = a;
  }
  char b = rec(vs[i].b, i, j);
  if (vs[i].op == "XOR") {
    return ms[s] = $xor(a, b);
  }
  if (vs[i].op == "AND") {
    return ms[s] = $and(a, b);
  }
  if (vs[i].op == "OR") {
    return ms[s] = $or(a, b);
  }
}
inline void solve() {
  vs.resize(n);
  for (int i = 0; i < n; i++) {
    parse(ss[i], vs[i]);
  }
  string m1, m2;
  m1 = m2 = string(m, '0');
  for (int j = 0; j < m; j++) {
    ms["?"] = '0';
    for (int i = 0; i < n; i++) {
      ms[vs[i].name] = rec(vs[i].name, i, j);
    }
    int s0 = 0;
    for (int i = 0; i < n; i++) {
      s0 += ms[vs[i].name] == '1';
      ms.erase(vs[i].name);
    }
    ms["?"] = '1';
    for (int i = 0; i < n; i++) {
      ms[vs[i].name] = rec(vs[i].name, i, j);
    }
    int s1 = 0;
    for (int i = 0; i < n; i++) {
      s1 += ms[vs[i].name] == '1';
      ms.erase(vs[i].name);
    }
    if (s0 <= s1) {
      m1[j] = '0';
    } else {
      m1[j] = '1';
    }
    if (s0 >= s1) {
      m2[j] = '0';
    } else {
      m2[j] = '1';
    }
  }
  cout << m1 << "\n" << m2 << "\n";
}
inline void read() {
  cin >> n >> m;
  while (cin.peek() != '\n') {
    cin.ignore();
  }
  cin.ignore();
  ss.resize(n);
  for (int i = 0; i < n; i++) {
    while (cin.peek() != '\n') {
      ss[i] += char(cin.peek());
      cin.ignore();
    }
    cin.ignore();
  }
}
