#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;
const int N = 3e3;
int nxt = 0, rval;
vector<string> res;
map<string, int> num;
string val[N], back[N];
map<string, string> pro;
const string op = "$^#&";
void split(const string &s, string &l, string &r, string &o, bool &f) {
  for (int i = int(0); i < int(int((s).size())); i++)
    for (int j = int(0); j < int(4); j++)
      if (s[i] == op[j])
        o = op[j], l = s.substr(0, i), r = s.substr(i + 1), f = 0;
}
string process(int x) {
  string s = val[x];
  if (pro.count(s)) return pro[s];
  string l, r, o;
  bool f = 1;
  split(s, l, r, o, f);
  if (f) {
    if (to_string(x) == s) {
      r = pro[s] = back[x];
      if (x == rval) res.emplace_back("res=" + r);
      return r;
    }
    if (x == rval) rval = stoi(s);
    return pro[s] = process(stoi(s));
  }
  string proc = process(stoi(l)) + o + process(stoi(r));
  bool done = pro.count(proc);
  r = done ? pro[proc] : pro[proc] = "a" + to_string(nxt++);
  if (!done) res.emplace_back((x == rval ? "res" : r) + "=" + proc);
  return pro[s] = r;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string t[n];
  for (int i = int(0); i < int(n); i++) cin >> t[i];
  for (int i = int(0); i < int(n); i++) {
    int x = int(t[i].find('=')), ne, nl, nr;
    string var = t[i].substr(0, x), exp = t[i].substr(x + 1), l, r, o;
    bool f = 1;
    split(exp, l, r, o, f);
    if (f) {
      if (!num.count(exp))
        ne = num[exp] = nxt++, val[ne] = to_string(ne), back[ne] = exp;
      val[num[var] = nxt++] = to_string(num[exp]);
    } else {
      if (!num.count(l))
        nl = num[l] = nxt++, val[nl] = to_string(nl), back[nl] = l;
      if (!num.count(r))
        nr = num[r] = nxt++, val[nr] = to_string(nr), back[nr] = r;
      val[num[var] = nxt++] = to_string(num[l]) + o + to_string(num[r]);
    }
  }
  if (!num.count("res")) return cout << "0\n", 0;
  nxt = 0, rval = num["res"], process(rval);
  int sz = int((res).size());
  if (sz == 1 && res[0] == "res=res") return cout << "0\n", 0;
  cout << sz << '\n';
  for (int j = int(0); j < int(sz); j++) cout << res[j] << '\n';
  return 0;
}
