#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  int v = 0, f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = (c & 15);
  while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
  x = v * f;
}
inline void read(long long &x) {
  long long v = 0ll, f = 1ll;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = (c & 15);
  while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
  x = v * f;
}
inline void readc(char &x) {
  char c;
  while (((c = getchar()) == ' ') || c == '\n')
    ;
  x = c;
}
const string opch = "$^#&";
int n, i, j, op[1005], cnt, tmp, vis[4005];
map<pair<int, int>, int> mp[4];
map<char, int> opid;
string name[4005], s1[1005], s2[4005], s3[4005];
map<string, int> cur;
int lc[4005], rc[4005], opt[4005];
vector<string> ans;
string rnd() {
  string st;
  int x = rand() % 26;
  st.push_back(x + 'a');
  x = rand() % 36;
  if (x < 26)
    st.push_back(x + 'a');
  else
    st.push_back(x - 26 + '0');
  x = rand() % 36;
  if (x < 26)
    st.push_back(x + 'a');
  else
    st.push_back(x - 26 + '0');
  x = rand() % 36;
  if (x < 26)
    st.push_back(x + 'a');
  else
    st.push_back(x - 26 + '0');
  return st;
}
void upd(string st) {
  if (!cur.count(st)) {
    cur[st] = ++cnt;
    name[cnt] = st;
  }
}
int f(int x, int a, int b) {
  if (mp[x].count(make_pair(a, b))) return mp[x][make_pair(a, b)];
  lc[tmp + 1] = a;
  rc[tmp + 1] = b;
  opt[tmp + 1] = x;
  return mp[x][make_pair(a, b)] = ++tmp;
}
void dfs(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  if (lc[x]) {
    dfs(lc[x]);
    dfs(rc[x]);
    ans.push_back(name[x] + "=" + name[lc[x]] + opch[opt[x]] + name[rc[x]]);
  }
}
int main() {
  memset(opt, -1, sizeof(opt));
  srand(time(NULL));
  ios_base::sync_with_stdio(false);
  ;
  opid['$'] = 0;
  opid['^'] = 1;
  opid['#'] = 2;
  opid['&'] = 3;
  cin >> n;
  for (((j)) = (1); ((j)) <= ((n)); ((j))++) {
    string st;
    cin >> st;
    string s1, s2, s3;
    s1 = st.substr(0, st.find('='));
    st = st.substr(st.find('=') + 1);
    for (((i)) = (0); ((i)) <= (((int)(st.size())) - 1); ((i))++) {
      if (!islower(st[i]) && !isupper(st[i]) && !isdigit(st[i])) {
        break;
      }
      s2.push_back(st[i]);
    }
    if (i == st.size()) {
      op[j] = -1;
    } else {
      op[j] = opid[st[i]];
      s3 = st.substr(i + 1);
    }
    ::s1[j] = s1;
    ::s2[j] = s2;
    ::s3[j] = s3;
  }
  for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
    upd(s1[i]);
    upd(s2[i]);
    if (op[i] != -1) upd(s3[i]);
  }
  tmp = cnt;
  for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
    string st;
    st = rnd();
    while (cur.count(st)) st = rnd();
    upd(st);
  }
  for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
    if (op[i] == -1) {
      cur[s1[i]] = cur[s2[i]];
    } else {
      cur[s1[i]] = f(op[i], cur[s2[i]], cur[s3[i]]);
    }
  }
  if (!cur.count("res")) {
    puts("0");
    return 0;
  }
  string tt = name[cur["res"]];
  name[cur["res"]] = "res";
  dfs(cur["res"]);
  if (ans.empty() && tt != "res") {
    printf("1\nres=%s\n", tt.c_str());
    return 0;
  }
  printf("%d\n", ans.size());
  for (__typeof((ans).begin()) it = (ans).begin(); it != (ans).end(); it++)
    puts(it->c_str());
  return 0;
}
