#include <bits/stdc++.h>
using namespace std;
int N, M;
int ar[10005][1005];
pair<pair<int, int>, int> opr[10005];
map<string, int> mp;
int tmp[10005][1005];
inline int get(int a, int num) {
  if (ar[a][num] != -1) return ar[a][num];
  int n = opr[a].second;
  int a1 = opr[a].first.first;
  int a2 = opr[a].first.second;
  if (n == 0) return ar[a][num] = get(a1, num) & get(a2, num);
  if (n == 1) return ar[a][num] = get(a1, num) | get(a2, num);
  if (n == 2) return ar[a][num] = get(a1, num) ^ get(a2, num);
}
int c = 0;
map<int, string> back;
void parse() {
  for (int i = 0; i < N; i++) {
    string a;
    cin >> a;
    if (!mp[a]) {
      mp[a] = c++;
      back[c - 1] = a;
    }
    string b;
    cin >> b;
    cin >> b;
    if ((b[0] - '0') <= 1) {
      for (int j = 0; j < M; j++) ar[mp[a]][j] = b[j] - '0';
      continue;
    }
    if (!mp[b]) {
      mp[b] = c++;
      back[c - 1] = b;
    }
    string op;
    cin >> op;
    int tmp1 = 0;
    if (op == "AND") tmp1 = 0;
    if (op == "OR") tmp1 = 1;
    if (op == "XOR") tmp1 = 2;
    string d;
    cin >> d;
    if (!mp[d]) {
      mp[d] = c++;
      back[c - 1] = d;
    }
    opr[mp[a]] = make_pair(make_pair(mp[b], mp[d]), tmp1);
  }
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  for (int i = 0; i < 2 * N; i++)
    for (int j = 0; j < M; j++) ar[i][j] = -1;
  parse();
  for (int i = 0; i < 2 * N; i++)
    for (int j = 0; j < M; j++) tmp[i][j] = ar[i][j];
  string tv;
  tv += '?';
  if (!mp[tv]) {
    for (int i = 0; i < M; i++) printf("0");
    printf("\n");
    for (int i = 0; i < M; i++) printf("0");
    printf("\n");
    return 0;
  }
  int cur = mp[tv];
  string res1;
  for (int i = 0; i < M; i++) {
    ar[cur][i] = 0;
    int cnt = 0;
    for (int j = 0; j < c; j++)
      if (j != cur) cnt += get(j, i);
    for (int j = 0; j < c; j++) ar[j][i] = tmp[j][i];
    ar[cur][i] = 1;
    int cnt2 = 0;
    for (int j = 0; j < c; j++)
      if (j != cur) cnt2 += get(j, i);
    if (cnt >= cnt2)
      ar[cur][i] = 0;
    else
      ar[cur][i] = 1;
    res1 += (char)(ar[cur][i] + 48);
  }
  string res2;
  for (int i = 0; i < c; i++)
    for (int j = 0; j < M; j++) ar[i][j] = tmp[i][j];
  for (int i = 0; i < M; i++) {
    ar[cur][i] = 0;
    int cnt = 0;
    for (int j = 0; j < c; j++)
      if (j != cur) cnt += get(j, i);
    for (int j = 0; j < c; j++) ar[j][i] = tmp[j][i];
    ar[cur][i] = 1;
    int cnt2 = 0;
    for (int j = 0; j < c; j++)
      if (j != cur) cnt2 += get(j, i);
    if (cnt <= cnt2)
      ar[cur][i] = 0;
    else
      ar[cur][i] = 1;
    res2 += (char)(ar[cur][i] + 48);
  }
  for (int i = 0; i < M; i++) printf("%c", res2[i]);
  printf("\n");
  for (int i = 0; i < M; i++) printf("%c", res1[i]);
  printf("\n");
}
