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 \le n \le 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

Source: codeforces | Difficulty: VERY_HARD
Tags: String algorithms

Graph algorithms

Implementation

Greedy algorithms


Solutions:
from random import seed, randint
import sys
sys.setrecursionlimit(10000)
opr = ['#', '^', '&', '$']
namespace = {'res': (False, 'res')}
rules = dict()
lookup = dict()
cnt = -1

def get_tag(var):
	if var in namespace:
		return namespace[var][1]
	else:
		return var
N = int(input())
for _ in range(N):
	(lval, rval) = input().split('=')
	for c in opr:
		if c in rval:
			(arg1, arg2) = map(get_tag, rval.split(c))
			rule = (arg1, arg2, c)
			if rule in rules:
				namespace[lval] = (True, rules[rule])
			else:
				cnt += 1
				namespace[lval] = (True, cnt)
				rules[rule] = cnt
				lookup[cnt] = rule
			break
	else:
		if rval in namespace:
			namespace[lval] = namespace[rval]
		else:
			namespace[lval] = (False, rval)
if namespace['res'] == (False, 'res'):
	print('0')
	exit()
program = []
myvars = dict()

def reserve():
	return ''.join((chr(randint(0, 25) + ord('a')) for _ in range(4)))

def implement(rule, final):
	if type(rule) == str:
		return rule
	elif rule in myvars:
		return myvars[rule]
	else:
		if final:
			name = 'res'
		else:
			name = reserve()
		myvars[rule] = name
		(arg1, arg2, op) = lookup[rule]
		(var1, var2) = (implement(arg1, False), implement(arg2, False))
		program.append(name + '=' + var1 + op + var2)
		return name
seed(123)
if namespace['res'][0]:
	implement(namespace['res'][1], True)
else:
	program.append('res=' + namespace['res'][1])
print(len(program))
print('\n'.join(program))


import sys
sys.setrecursionlimit(65536)
(vars, reserved) = ({}, set())
(rules, irules, R) = ([], {}, 0)

def wwj():
	n__ = 0

	def dak():
		nonlocal n__
		while 'a' + str(n__) in reserved:
			n__ += 1
		n__ += 1
		return 'a' + str(n__ - 1)
	return dak
next_var = wwj()
L = int(input())
for i in range(L):
	(k, v) = input().split('=')
	if v.isidentifier():
		if v in vars:
			vars[k] = vars[v]
		else:
			vars[k] = v
			reserved.add(v)
	else:
		j = [i for i in range(len(v)) if not v[i].isalnum()][0]
		(v1, op, v2) = (vars.get(v[:j], v[:j]), v[j], vars.get(v[j + 1:], v[j + 1:]))
		r = (op, v1, v2)
		if r not in irules:
			rules.append(r)
			irules[r] = R
			R += 1
		vars[k] = irules[r]
if vars.get('res', 'res') == 'res':
	print(0)
	exit()
resolve = {}
output = []

def interprete(v, name=None):
	if isinstance(v, str):
		return v
	if v in resolve:
		return resolve[v]
	(op, v1, v2) = rules[v]
	(u1, u2) = (interprete(v1), interprete(v2))
	if name is None:
		name = next_var()
	resolve[v] = name
	output.append('{}={}{}{}'.format(name, u1, op, u2))
	return name
R = interprete(vars['res'], 'res')
if R != 'res':
	output.append('res={}'.format(R))
print(len(output), '\n'.join(output), sep='\n')