#K87992. Taco's Ingredient Aggregator
Taco's Ingredient Aggregator
Taco's Ingredient Aggregator
In this problem, you are given (T) test cases. Each test case consists of a series of potion recipes. For each test case, the first line contains an integer (N) representing the number of recipes. Each of the next (N) lines contains a sequence of pairs, where each pair consists of an integer and an uppercase letter (representing the quantity and the ingredient respectively). Your task is to compute the total quantity required for each ingredient across all recipes in a test case and then output each ingredient along with its cumulative quantity, sorted in alphabetical order by the ingredient letter.
For example, if a test case is given as follows:
3
2 A 4 B 3 C
1 B 5 A
2 C 7 B 1 D
You should calculate:
Ingredient A: 2 + 5 = 7
Ingredient B: 4 + 1 + 7 = 12
Ingredient C: 3 + 2 = 5
Ingredient D: 1
The output should be:
A 7
B 12
C 5
D 1
inputFormat
The first line of input contains an integer (T), the number of test cases. Following this, for each test case, the first line contains an integer (N) indicating the number of recipes. Each of the next (N) lines contains a string representing a recipe. A recipe is a sequence of pairs where each pair consists of a positive integer and an uppercase letter, separated by spaces.
outputFormat
For each test case, output each ingredient and its total quantity on a separate line in the format Ingredient Quantity. The ingredients must be listed in alphabetical order. All test cases’ outputs are concatenated in the order they appear.## sample
4
3
2 A 4 B 3 C
1 B 5 A
2 C 7 B 1 D
1
10 X 20 Y 30 Z
1
100 A 100 B 100 C
1
3 C 2 A 4 B
A 7
B 12
C 5
D 1
X 10
Y 20
Z 30
A 100
B 100
C 100
A 2
B 4
C 3
</p>