This page is still under construction.

Parts of this page are still being built. What you see may change.

Report Recovery

Interview

Time limit3sMemory limit128 MB

Summary
Rebuild a space-stripped sales report by splitting each digit run into numbers, choosing the lexicographically smallest reconstruction consistent with the report's structure.
Level

Medium6 of 10

Topics
Backtracking, Brute force, Implementation, Greedy
Solved
No attempts yet

Statement

At the end of the week, John asked Mary to send him an urgent sales report. In a hurry to leave for her holiday, Mary copy-pasted the sales sheet into an email, sent it, then turned off her phone and left for a place where she would be unreachable for two weeks.

When John opened the message, he discovered that every space had been stripped out. Fortunately, he remembered the exact shape of the report:

  • The first line is a header: the product codes P1 P2 ... PN followed by the word Totals. Products are numbered consecutively from 1 to N.
  • Each following line (except the last) reports one seller. It starts with the seller's name (a single word of letters), then lists, in order, the number of units that seller sold of each of the N products, and ends with that seller's row total (the sum of those N numbers).
  • The last line starts with the marker TP, followed by the total sold of each product (the column sums over all sellers), and finally the grand total (the sum of those column totals).

All quantities are non-negative integers. A quantity of zero is written as a single 0, and a positive quantity has no leading zeros. No seller's name begins with the letters TP.

Because the spaces are gone, more than one report may be consistent with the same run of digits. Help John rebuild the report; when several reconstructions are possible, output the lexicographically smallest one (defined in Output).

Input

The first line contains an integer C, the number of reports. Each report is then given, with all spaces removed, on consecutive lines: a header line, one line per seller, and finally the TP line.

Constraints:

  • 1≤N≤51 \le N \le 5 (products per report);
  • at most 4 sellers per report;
  • each seller name has 1 to 10 letters (uppercase or lowercase);
  • each seller sold fewer than 1000 units of each product;
  • no seller name begins with TP.

Output

For each report, print the reconstructed report, one line per original line, with items separated by a single space and no trailing space. Do not print blank lines between reports.

If several reconstructions are consistent with the input, print the lexicographically smallest one. To compare two candidate reports, list all of their integers in reading order — for each seller row its N quantities followed by its row total, taken in the given seller order, then the totals row's column sums followed by the grand total — and compare these two integer sequences element by element; the smaller report is the one with the smaller value at the first position where they differ.

Examples2

  1. Example 1

    Input
    2
    P1P2P3Totals
    Amanda121100131
    Charles5141772
    Monique14121238
    TP1862629241
    P1P2Totals
    Ingrid9519851936
    Candid49212504
    Peter10313
    Camila000
    TP145310002453
    
    Expected output
    P1 P2 P3 Totals
    Amanda 121 10 0 131
    Charles 51 4 17 72
    Monique 14 12 12 38
    TP 186 26 29 241
    P1 P2 Totals
    Ingrid 951 985 1936
    Candid 492 12 504
    Peter 10 3 13
    Camila 0 0 0
    TP 1453 1000 2453
    
  2. Example 2

    Input
    1
    P1P2P3Totals
    Ana5102035
    Ben100200300600
    TP105210320635
    
    Expected output
    P1 P2 P3 Totals
    Ana 5 10 20 35
    Ben 100 200 300 600
    TP 105 210 320 635