#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
template <typename T>
void chmin(T &a, T b) {
  if (a > b) a = b;
}
template <typename T>
void chmax(T &a, T b) {
  if (a < b) a = b;
}
template <typename T>
void add(T &a, T b) {
  a += b;
  if (a < 0) a += MOD;
  if (a >= MOD) a -= MOD;
}
void FastIn(int &T) {
  char C;
  bool F = 0;
  for (; C = getchar(), C < '0' || C > '9';)
    if (C == '-') F = 1;
  for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0')
    ;
  F && (T = -T);
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
double din() {
  double x;
  scanf("%lf", &x);
  return x;
}
long long p2[64];
void initP2() {
  p2[0] = 1;
  for (int i = (1); i <= (63); ++i) {
    p2[i] = p2[i - 1] * 2;
  }
}
int countBitsBaseB(long long n, long long b) {
  if (n == 0) return 1;
  int ret = 0;
  while (n) {
    ret++;
    n /= b;
  }
  return ret;
}
int count1(long long n) {
  int ret = 0;
  while (n) {
    ret += n & 1;
    n >>= 1;
  }
  return ret;
}
long long c[1024][1024] = {0};
void initC() {
  for (int i = 0; i < (1024); ++i) {
    for (int j = 0; j < (i + 1); ++j) {
      if (j == 0 || j == i) {
        c[i][j] = 1;
      } else {
        c[i][j] = c[i - 1][j - 1];
        add(c[i][j], c[i - 1][j]);
      }
    }
  }
}
void init() {
  initP2();
  initC();
}
const long long INF = 1000000000;
int ansMin[1024];
int ansMax[1024];
struct CMD {
  int var;
  int varL;
  int varR;
  string op;
  bool isvar;
  string bin;
} cmd[5010];
int var[5010];
int GetValue(int id, int bit) {
  if (id == -1) return bit;
  return var[id];
}
int calc(int L, int R, const string &op) {
  if (op[0] == 'X')
    return L ^ R;
  else if (op[0] == 'O')
    return L | R;
  else if (op[0] == 'A')
    return L & R;
  return 0;
}
int GetSum(int n, int pos, int bit) {
  for (int i = 0; i < (n); ++i) {
    if (cmd[i].isvar) {
      var[cmd[i].var] = cmd[i].bin[pos] - '0';
    } else {
      int L = GetValue(cmd[i].varL, bit);
      int R = GetValue(cmd[i].varR, bit);
      var[cmd[i].var] = calc(L, R, cmd[i].op);
    }
  }
  int ret = 0;
  for (int i = 0; i < (5010); ++i) {
    ret += var[i];
  }
  return ret;
}
map<string, int> id2;
int GetId(string s) {
  if (s[0] == '?') return -1;
  if (id2.find(s) != id2.end()) return id2[s];
  int ret = id2.size();
  id2[s] = ret;
  return ret;
}
int main() {
  init();
  int n = in();
  int m = in();
  id2.clear();
  for (int i = 0; i < (n); ++i) {
    char nouse[1024] = {0};
    char var[1024] = {0};
    char varL[1024] = {0};
    char op[1024] = {0};
    char varR[1024] = {0};
    scanf("%s%s%s", var, nouse, varL);
    if (isdigit(varL[0]) == false) {
      cmd[i].isvar = false;
      scanf("%s%s", op, varR);
      cmd[i].var = GetId(var);
      cmd[i].varL = GetId(varL);
      cmd[i].varR = GetId(varR);
      cmd[i].op = string(op);
    } else {
      cmd[i].isvar = true;
      cmd[i].var = GetId(var);
      cmd[i].bin = string(varL);
    }
  }
  memset((var), (0), sizeof((var)));
  for (int i = 0; i < (m); ++i) {
    int sum0 = GetSum(n, i, 0);
    int sum1 = GetSum(n, i, 1);
    ansMin[i] = 0;
    if (sum1 < sum0) {
      ansMin[i] = 1;
    }
    ansMax[i] = 0;
    if (sum1 > sum0) {
      ansMax[i] = 1;
    }
  }
  for (int i = 0; i < (m); ++i) {
    printf("%d", ansMin[i]);
  }
  cout << endl;
  for (int i = 0; i < (m); ++i) {
    printf("%d", ansMax[i]);
  }
  cout << endl;
  return 0;
}
