Checkable Additions
Time limit5sMemory limit512 MB
From recorded equations of the form x+y=z, report each queried pair sum forced to one value by them.
- Level
Medium6 of 10
- Topics
- Union-find, Graph
- Solved
- No attempts yet
Problem
Six years ago a scientist with bad intentions, Alice, built a robot named Bob whose reasoning was that of an infant.
Bob is six years old now and goes to primary school. Addition is the first operation he learned in mathematics, and his reasoning is strong enough that he concludes from and .
Alice wanted to test Bob's addition. She gave him equations such as , , and asked for the answers to questions like and . She marked the answer sheet one question at a time and found no mistake, then spilled a cup of coffee over the page that held the original equations. What she still has is a list of questions Bob answered correctly, for example , , . She wants to keep marking the questions that are still checkable. Those three answers give , so the question is checkable.
To keep the robot out of Alice's hands, you took a job as her assistant. She now asks you which of the remaining questions are checkable and what their answers are.
A question is checkable when every assignment of values to the variables that agrees with the recorded answers gives the same value for .
Input
The first line holds the number of test cases, . test cases follow.
The first line of each test case holds one integer , the number of correctly answered questions. Each of the next lines holds one correctly answered question in the form x+y=z, where x and y are variable names and z is a decimal integer.
The next line holds one integer , the number of remaining questions. Each of the next lines holds one question in the form x+y, where x and y are variable names.
Limits
- A variable name is a string of lowercase English letters and holds at most 10 characters.
- The answered questions never contradict each other, and the answer to a checkable question is always an integer.
- The two names in an answered question or in a remaining question can be the same variable.
Output
For each test case, print a line Case #x:, where x is the test case number starting from 1. Then print one line for every checkable question of that test case, in the form x+y=z, where x and y are the variable names exactly as they appear in the question and z is the answer as a decimal integer.
Keep the questions in the order they were given in the input. Do not skip a repeated question, because Alice would fire you for pointing out a mistake of hers. A question that is not checkable produces no line.