My youngest sister Rimi came to me with a sad expression on her face. I was thinking what might be the reason, and she revealed it after a while - she was given a math homework and the homework was to solve a linear equation with one variable. Here is the equation she was given:
$$2 * x + 3 * 2 - (7 + 5) = x + 25$$
From this equation, she has to find the value of x. That's why she asked me to write a program to solve any type of linear equations with one variable. And since she is only a child, it's clear that she will not give any expression that may lead to a non-linear subexpressions like (x * x), or even she will not give any inputs that lead to a subexpression like (x * x - x * x + x). And she is not familiar with unary operators, so, she'll use binary operators only, but not division (or modulo) operators. But she may use brackets. So, before start coding, I wrote the following grammar:
Type | Declaration | |
---|---|---|
Expression | => | Term | Expression '+' Term | Expression '-' Term |
Term | => | Factor | Term '*' Factor |
Factor | => | Number | 'x' | '(' Expression ')' |
Number | => | Digit | Digit Number |
Digit | => | '0' | '1' | ... | '9' |
Table 1: Grammar in EBNF
Though the grammar can produce nonlinear subexpressions, but I am sure that she will not give any such inputs. And numbers will not contain leading zeroes. But when I started coding, the power was gone due to load shedding, that's why I am asking your help.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing a string E (1 ≤ length(E) ≤ 100) denoting an equation. The equation follows the above rules. No spaces will be given in the string. And the input is given such that, in any calculation, the absolute value of the result (or operand) will not exceed 109.
Output
For each case, print the case number first. Then if x has infinitely many solutions, print infinitely many solutions
. Print no solution
if there is no such solution. If x has only one solution, print x in p/q form if x is not an integer (p and q should be relatively prime, q > 0), otherwise just print x.
Sample
Sample Input | Sample Output |
---|---|
2 x*5+31=7-(2*20)+53 x*2+(3*x-7)=2*x+(x-5)+(2*x-2) | Case 1: -11/5 Case 2: infinitely many solutions |