#include <bits/stdc++.h>
struct oper {
  std ::string num;
  int x, opt, y;
};
template <class T>
T Min(const T &a, const T &b) {
  return a < b ? a : b;
}
template <class T>
T Max(const T &a, const T &b) {
  return a > b ? a : b;
}
template <class T>
bool Chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
bool Chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int SN = 5000 + 50;
std ::string str, ans1, ans2;
oper op[SN];
int n, m, t;
std ::map<std ::string, int> mp;
bool bit[SN];
void In();
int Calc(int, int);
int Calc(int, int, int, int);
int main() {
  int x, y, z, flag = 0;
  In();
  for (int i = m - 1; i >= 0; --i) {
    x = Calc(i, 0);
    y = Calc(i, 1);
    if (x == y)
      ans1.push_back('0'), ans2.push_back('0');
    else if (x < y)
      ans1.push_back('0'), ans2.push_back('1');
    else
      ans1.push_back('1'), ans2.push_back('0');
  }
  std ::cout << ans1 << std ::endl << ans2 << std ::endl;
  return 0;
}
void In() {
  mp.insert(std::pair<std::string, int>("?", -1));
  scanf("%d%d", &n, &m);
  for (auto i = (0); i != (n); ++i) {
    std ::cin >> str;
    mp.insert(std::pair<std::string, int>(str, t++));
    std ::cin >> str >> str;
    if (mp.count(str))
      op[i].x = mp[str];
    else {
      op[i].num = str, op[i].x = -2;
      continue;
    }
    std ::cin >> str;
    if (str == "XOR")
      std ::cin >> str, op[i].y = mp[str], op[i].opt = 0;
    else if (str == "AND")
      std ::cin >> str, op[i].y = mp[str], op[i].opt = 1;
    else
      std ::cin >> str, op[i].y = mp[str], op[i].opt = 2;
  }
}
int Calc(int k, int x) {
  int res = 0;
  for (auto i = (0); i != (n); ++i)
    if (op[i].x == -2)
      bit[i] = op[i].num[m - k - 1] - '0';
    else
      bit[i] = Calc(op[i].x, op[i].opt, op[i].y, x);
  for (auto i = (0); i != (n); ++i) res += bit[i];
  return res;
}
int Calc(int x, int opt, int y, int z) {
  x = x == -1 ? z : bit[x];
  y = y == -1 ? z : bit[y];
  if (opt == 0)
    return x ^ y;
  else if (opt == 1)
    return x & y;
  else
    return x | y;
}
