#include <bits/stdc++.h>
using namespace std;
long long int mul(long long int x, long long int y) {
  long long int ans = 1;
  while (y > 0) {
    if (y & 1) ans = (ans * x) % 1000000007;
    x = (x * x) % 1000000007;
    y /= 2;
  }
  return ans;
};
long long int dp[5005][1005][2], ans1[1005], ans2[1005];
int main() {
  long long int aa, bb, n, m, i, j;
  cin >> n >> m;
  string str, tmp, str1, str2;
  map<string, long long int> mp;
  for (i = 1; i <= n; i++) {
    cin >> str >> tmp;
    mp[str] = i;
    cin >> str1;
    if (str1[0] == '1' || str1[0] == '0')
      for (j = 1; j <= m; j++) dp[i][j][0] = dp[i][j][1] = str1[j - 1] - '0';
    else {
      cin >> tmp >> str2;
      for (j = 1; j <= m; j++) {
        if (str1[0] == '?')
          aa = 0;
        else
          aa = dp[mp[str1]][j][0];
        if (str2[0] == '?')
          bb = 0;
        else
          bb = dp[mp[str2]][j][0];
        if (tmp[0] == 'X')
          dp[i][j][0] = aa ^ bb;
        else if (tmp[0] == 'A')
          dp[i][j][0] = aa & bb;
        else
          dp[i][j][0] = aa | bb;
        if (str1[0] == '?')
          aa = 1;
        else
          aa = dp[mp[str1]][j][1];
        if (str2[0] == '?')
          bb = 1;
        else
          bb = dp[mp[str2]][j][1];
        if (tmp[0] == 'X')
          dp[i][j][1] = aa ^ bb;
        else if (tmp[0] == 'A')
          dp[i][j][1] = aa & bb;
        else
          dp[i][j][1] = aa | bb;
      }
    }
  }
  for (j = 1; j <= m; j++) {
    aa = 0, bb = 0;
    for (i = 1; i <= n; i++) {
      if (dp[i][j][0]) ++aa;
      if (dp[i][j][1]) ++bb;
    }
    if (aa <= bb)
      ans1[j] = 0;
    else
      ans1[j] = 1;
    if (aa >= bb)
      ans2[j] = 0;
    else
      ans2[j] = 1;
  }
  for (i = 1; i <= m; i++) cout << ans1[i];
  cout << "\n";
  for (i = 1; i <= m; i++) cout << ans2[i];
  return 0;
}
