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;
const int N = 4000;
vector<string> vec;
map<string, int> id;
int n, nod = 1, number[N];
bool vis[N];
short Memory[N][N][4];
string cmd;
struct data {
  string name;
  int ld, rd;
  char op;
} d[N];
inline int exc(char c) {
  switch (c) {
    case '$':
      return 0;
    case '#':
      return 1;
    case '^':
      return 2;
    case '&':
      return 3;
  }
  return 0;
}
inline string get_name() {
  bool qwq = 1;
  string s;
  do {
    s = "";
    int len = rand() % 3 + 2;
    while (len--)
      s += (char)(rand() % 2 ? (rand() % 26 + 'a') : (rand() % 26 + 'A'));
    qwq = id.count(s);
  } while (qwq);
  return s;
}
void dfs(int now, bool tag = 0) {
  if (vis[now]) return;
  vis[now] = 1;
  if (d[now].op == 0) {
    number[now] = id[d[now].name] = now;
  } else if (d[now].op == 1) {
    dfs(d[now].ld);
    number[now] = number[d[now].ld];
  } else {
    dfs(d[now].ld), dfs(d[now].rd);
    int numL = number[d[now].ld], numR = number[d[now].rd];
    if (short x = Memory[numL][numR][exc(d[now].op)]) {
      number[now] = x;
      return;
    }
    if (!tag) {
      string str = get_name();
      id[str] = ++nod;
      number[now] = nod;
      d[nod].name = str;
      d[nod].op = d[now].op;
    } else
      number[now] = 1;
    vec.push_back(d[number[now]].name + '=' + d[numL].name + d[now].op +
                  d[numR].name);
    Memory[numL][numR][exc(d[now].op)] = number[now];
  }
}
int main() {
  srand(time(0));
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  id["res"] = 1;
  d[1].name = "res";
  cin >> n;
  while (n--) {
    cin >> cmd;
    char op = 0;
    for (int i = 0; i < cmd.size(); ++i)
      if (cmd[i] == '#' || cmd[i] == '^' || cmd[i] == '$' || cmd[i] == '&')
        op = cmd[i], cmd[i] = ' ';
      else if (cmd[i] == '=')
        cmd[i] = ' ';
    string l, r, k;
    istringstream in(cmd);
    in >> k >> l;
    if (op) {
      in >> r;
      if (!id.count(l)) {
        id[l] = ++nod;
        d[nod].name = l;
      }
      if (!id.count(r)) {
        id[r] = ++nod;
        d[nod].name = r;
      }
      int L = id[l], R = id[r];
      id[k] = ++nod;
      d[nod].name = k;
      d[nod].ld = L;
      d[nod].rd = R;
      d[nod].op = op;
    } else {
      if (!id.count(l)) {
        id[l] = ++nod;
        d[nod].name = l;
      }
      int L = id[l];
      id[k] = ++nod;
      d[nod].name = k;
      d[nod].ld = L;
      d[nod].op = 1;
    }
  }
  int rt;
  dfs(rt = id["res"], 1);
  if (number[rt] != 1) {
    if (!d[number[rt]].op)
      vec.push_back("res=" + d[number[rt]].name);
    else {
      if (vec.empty()) return 1;
      string s = vec.back();
      vec.pop_back();
      reverse(s.begin(), s.end());
      while (s.back() != '=') s.pop_back();
      s += "ser";
      reverse(s.begin(), s.end());
      vec.push_back(s);
    }
  }
  cout << vec.size() << '\n';
  for (auto& i : vec) cout << i << '\n';
  return 0;
}