Problem: Let's analyze a program written on some strange programming language. The variables in this language have names consisting of 1 to 4 characters, and each character is a lowercase or an uppercase Latin letter, or a digit. There is an extra constraint that the first character should not be a digit.

There are four types of operations in the program, each denoted by one of the characters: $, ^, # or &.

Each line of the program has one of the following formats: 

  * <lvalue>=<rvalue>, where <lvalue> and <rvalue> are valid variable names; 
  * <lvalue>=<arg1><op><arg2>, where <lvalue>, <arg1> and <arg2> are valid variable names, and <op> is an operation character. 



The program is executed line-by-line, and the result of execution is stored in a variable having the name res. If res is never assigned in the program, then the result will be equal to the value of res before running the program.

Two programs are called equivalent if no matter which operations do characters $, ^, # and & denote (but, obviously, performing the same operation on the same arguments gives the same result) and which values do variables have before execution of program, the value of res after running the first program is equal to the value of res after running the second program (the programs are executed independently).

You are given a program consisting of n lines. Your task is to write a program consisting of minimum possible number of lines that is equivalent to the program you are given.

Input

The first line contains one integer n (1 ≤ n ≤ 1000) — the number of lines in the program.

Then n lines follow — the program itself. Each line corresponds to the format described in the statement and has no extra whitespaces.

Output

In the first line print k — the minimum number of lines in the equivalent program.

Then print k lines without any whitespaces — an equivalent program having exactly k lines, in the same format it is described in the statement.

Examples

Input


4
c=aa#bb
d12=c
res=c^d12
tmp=aa$c


Output


2
aaaa=aa#bb
res=aaaa^aaaa


Input


2
max=aaaa$bbbb
min=bbbb^aaaa


Output


0

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXLINE = 1000;
const int MAXLINELEN = 4 + 1 + 4 + 1 + 4 + 1;
struct Node {
  int a;
  char op;
  int b;
  Node() {}
  Node(int a, char op, int b) : a(a), op(op), b(b) {}
};
bool operator==(const Node &p, const Node &q) {
  return p.a == q.a && p.op == q.op && p.b == q.b;
}
int nline;
char line[MAXLINE][MAXLINELEN + 1];
int nans;
char ans[MAXLINE][MAXLINELEN + 1];
vector<string> names;
map<string, int> mp;
int lhs[MAXLINE], lpar[MAXLINE];
char op[MAXLINE];
int rpar[MAXLINE];
vector<Node> vars;
vector<int> curvar;
vector<bool> valid;
vector<bool> used;
vector<string> newnames;
int lookup(string s) {
  auto it = mp.find(s);
  if (it != mp.end()) return it->second;
  int ret = ((int)(names).size());
  names.push_back(s);
  mp[s] = ret;
  return ret;
}
void printvar(int idx) {
  if (vars[idx].op == '=')
    printf("%s", names[vars[idx].a].c_str());
  else {
    printf("(");
    printvar(vars[idx].a);
    printf("%c", vars[idx].op);
    printvar(vars[idx].b);
    printf(")");
  }
}
void solve() {
  names.clear();
  mp.clear();
  int resname = lookup("res");
  for (int i = (0); i < (nline); ++i) {
    int eqidx = -1, opidx = -1, linelen = strlen(line[i]);
    for (int j = (0); j < (linelen); ++j)
      if (line[i][j] == '=') {
        assert(eqidx == -1);
        eqidx = j;
      } else if (line[i][j] == '$' || line[i][j] == '^' || line[i][j] == '#' ||
                 line[i][j] == '&') {
        assert(opidx == -1);
        opidx = j;
      }
    assert(eqidx != -1);
    if (opidx == -1) {
      string a = string(line[i] + 0, line[i] + eqidx),
             b = string(line[i] + eqidx + 1, line[i] + linelen);
      lhs[i] = lookup(a);
      lpar[i] = lookup(b);
      op[i] = '=';
    } else {
      string a = string(line[i] + 0, line[i] + eqidx),
             b = string(line[i] + eqidx + 1, line[i] + opidx),
             c = string(line[i] + opidx + 1, line[i] + linelen);
      lhs[i] = lookup(a);
      lpar[i] = lookup(b);
      op[i] = line[i][opidx];
      rpar[i] = lookup(c);
    }
  }
  vars.clear();
  for (int i = (0); i < (((int)(names).size())); ++i)
    vars.push_back(Node(i, '=', -1));
  curvar = vector<int>(((int)(names).size()));
  for (int i = (0); i < (((int)(curvar).size())); ++i) curvar[i] = i;
  for (int i = (0); i < (nline); ++i) {
    if (op[i] == '=') {
      curvar[lhs[i]] = curvar[lpar[i]];
    } else {
      int id = ((int)(vars).size());
      vars.push_back(Node(curvar[lpar[i]], op[i], curvar[rpar[i]]));
      curvar[lhs[i]] = id;
    }
  }
  int resvar = curvar[resname];
  valid = vector<bool>(((int)(vars).size()), true);
  for (int i = (0); i < (((int)(vars).size())); ++i)
    if (valid[i]) {
      for (int j = (i + 1); j < (((int)(vars).size())); ++j)
        if (valid[j] && vars[j] == vars[i]) {
          valid[j] = false;
          if (resvar == j) resvar = i;
          for (int k = (j + 1); k < (((int)(vars).size())); ++k) {
            if (vars[k].a == j) vars[k].a = i;
            if (vars[k].b == j) vars[k].b = i;
          }
        }
    }
  used = vector<bool>(((int)(vars).size()), false);
  queue<int> q;
  used[resvar] = true;
  q.push(resvar);
  while (!q.empty()) {
    int at = q.front();
    q.pop();
    if (vars[at].op == '=') continue;
    if (!used[vars[at].a]) used[vars[at].a] = true, q.push(vars[at].a);
    if (!used[vars[at].b]) used[vars[at].b] = true, q.push(vars[at].b);
  }
  newnames = vector<string>(((int)(vars).size()));
  int nxtnewname = 0;
  set<string> reservednames;
  reservednames.insert("res");
  if (vars[resvar].op != '=') newnames[resvar] = "res";
  for (int i = (0); i < (((int)(vars).size())); ++i)
    if (valid[i] && used[i] && vars[i].op == '=')
      newnames[i] = names[vars[i].a], reservednames.insert(newnames[i]);
  for (int i = (0); i < (((int)(vars).size())); ++i)
    if (valid[i] && used[i] && i != resvar && vars[i].op != '=')
      while (true) {
        int a = nxtnewname / 26 / 27, b = nxtnewname / 26 % 27,
            c = nxtnewname % 26;
        ++nxtnewname;
        string name;
        if (a != 0) name += string(1, 'a' + a - 1);
        if (b != 0) name += string(1, 'a' + b - 1);
        name += string(1, 'a' + c);
        if (reservednames.count(name)) continue;
        newnames[i] = name;
        break;
      }
  nans = 0;
  for (int i = (0); i < (((int)(vars).size())); ++i)
    if (valid[i] && used[i] && vars[i].op != '=') {
      string cur = newnames[i] + '=' + newnames[vars[i].a] + vars[i].op +
                   newnames[vars[i].b];
      sprintf(ans[nans++], "%s", cur.c_str());
    }
  if (vars[resvar].op == '=' && newnames[vars[resvar].a] != "res") {
    string cur = "res=" + newnames[vars[resvar].a];
    sprintf(ans[nans++], "%s", cur.c_str());
  }
}
void run() {
  scanf("%d", &nline);
  for (int i = (0); i < (nline); ++i) scanf("%s", line[i]);
  solve();
  printf("%d\n", nans);
  for (int i = (0); i < (nans); ++i) printf("%s\n", ans[i]);
}
int main() {
  run();
  return 0;
}