A symbolic system of equations is a set of equations , where , , …, are variables and is a symbol representing an arbitrary function with arguments (we say that has arity ). A solution of a system with variables is any assignment of expressions to variables in such a way that for every equation it holds that .
Write a program that, given a system of symbolic equations, computes its most general solution, or tells that it does not exist.
Input consists of several cases, each with , followed by variables in lexicographical order, followed by the number of equations , followed by equations in the exact format of the examples. Variables and functions are words made up of lowercase letters, all different. Every variable appears at most once in the left side of an equation. Every function can occur several times, but always with the same arity, between 1 and . All arguments of the same function are different variables. You can assume .
Print, in lexicographical order of the variables, the most general solution of the system, following the format of the examples. Print an empty line at the end of each case.
Take inspiration from a topological sort.
Input
3 x y z 2 z = f ( x y ) y = h ( x ) 1 x 1 x = f ( x ) 2 xx yy 2 xx = ff ( yy ) yy = ff ( xx ) 2 abc z 0 6 uu uv w x y z 4 x = f ( uv ) y = gg ( x z ) w = gg ( uv y ) uu = gh ( uv )
Output
x -> x y -> h ( x ) z -> f ( x h ( x ) ) No solution! No solution! abc -> abc z -> z uu -> gh ( uv ) uv -> uv w -> gg ( uv gg ( f ( uv ) z ) ) x -> f ( uv ) y -> gg ( f ( uv ) z ) z -> z