#include <bits/stdc++.h>
using namespace std;
const int N = 1023;
vector<int> vec[N];
map<string, int> mmp;
map<string, unsigned long long> key;
vector<string> ans;
unsigned long long Key[N];
map<unsigned long long, string> calced;
set<string> used;
string a[N], b[N], c[N];
char op[N];
string solve(string res, int n) {
  if (mmp.find(res) == mmp.end()) return res;
  int p = mmp[res];
  auto it = lower_bound(vec[p].begin(), vec[p].end(), n);
  if (it == vec[p].begin()) return res;
  n = *--it;
  if (calced.find(Key[n]) != calced.end()) return calced[Key[n]];
  if (op[n] == '=')
    return calced[Key[n]] = solve(b[n], n);
  else {
    string ret("test");
    static char ran[] = "qwertyuiopasdfghjklzxcvbnm1234567890";
    while (1) {
      ret[0] = 'a' + rand() % 26;
      for (int i = 1; i < 4; ++i) ret[i] = ran[rand() % 36];
      if (!used.count(ret)) {
        used.insert(ret);
        break;
      }
    }
    ans.push_back(ret + '=' + solve(b[n], n) + op[n] + solve(c[n], n));
    return calced[Key[n]] = ret;
  }
}
unsigned long long calc(unsigned long long a, unsigned long long b, char c) {
  return (a + c * 3131441) * (b + c * 2332424);
}
int main() {
  int n, s = 0;
  cin >> n;
  for (int i = 1, p; i <= n; ++i) {
    string str;
    cin >> str;
    for (int j = p = 0; j < str.size(); ++j) {
      if (str[j] == '=')
        p = j, a[i] = str.substr(0, j);
      else if (str[j] == '$' || str[j] == '^' || str[j] == '#' || str[j] == '&')
        b[i] = str.substr(p + 1, j - p - 1),
        c[i] = str.substr(j + 1, str.size() - j - 1), op[i] = str[j];
    }
    if (!op[i]) op[i] = '=', b[i] = str.substr(p + 1, str.size() - p - 1);
    if (!mmp.count(a[i])) mmp[a[i]] = ++s;
    if (!key.count(b[i])) key[b[i]] = rand() * rand();
    if (!c[i].empty() && !key.count(c[i])) key[c[i]] = rand() * rand();
    if (op[i] == '=')
      key[a[i]] = key[b[i]];
    else
      key[a[i]] = calc(key[b[i]], key[c[i]], op[i]);
    Key[i] = key[a[i]];
    used.insert(a[i]);
    used.insert(b[i]);
    used.insert(c[i]);
    vec[mmp[a[i]]].push_back(i);
  }
  for (int i = 1; i <= s; ++i) sort(vec[i].begin(), vec[i].end());
  string r = solve("res", n + 1);
  if (r != "res") {
    for (int i = ans.size() - 1; ~i; --i)
      if (~ans[i].find(r)) {
        ans[i].replace(0, r.size(), "res");
        cout << ans.size() << '\n';
        for (auto i : ans) cout << i << "\n";
        return 0;
      }
    ans.push_back("res=" + r);
  }
  cout << ans.size() << '\n';
  for (auto i : ans) cout << i << '\n';
}
