Problem E  Pay Up

A group of students and coaches went to St John for a contest. During the
trip various members paid bills that should have been shared by other
members. At the end of the trip a settlement needs to be arranged. Shown
below is sample data file E.in (7 lines).

 400 A C E C
 200 B D F
-200 G E A

-4 A Dept
 5 A B C A B
-6 B Dept

The input will contain one or more data sets separated by empty lines.
There are 2 data sets in this example. Each data set has one or more
lines. Each line has an amount followed by the name of the person who
paid it and one or more names of others who should have shared the
expense. In the first line above A paid 400 but C and E shared the
benefit. Since C's name occurs twice C is responsible for two fourths of
the 400 and A and E one fourth each.  When the amount is negative as in
the third line that means the person paid a positive amount but is not
responsible for a share. The following names are. In the third line G
paid 200. E and A both owe 100 for that one and G is owed 200.

When we calculate each share of an expense we round to the nearest
dollar. Thus for the second line B, D and F each owe one third of 200
which we round to 67. Because of rounding, after all settlements are
made, some may end up with a small amount too much or too little.

The output for the above example should be (8 lines):

C pays A 200
E pays G 200
D pays B 67
F pays B 66

C pays A 1
Dept pays A 6
Dept pays B 4

In what follows an "ower" is someone who still owes and an "owee" is
someone who is still owed.

The format of the output consists of one settlement for each data set
with an empty line between settlements. Each settlement has two stages
each of which produces zero or more lines of output.  In the first stage,
in the interest of reducing the number of payments, owers seek out owees
who are owed exactly the same amount and pay them this amount. This
explains the first two lines of output. After reading the first data set
we find that C and E both owe 200 and A and G are both owed 200. When
there is a choice about who pays whom, as there is in this example, the
alphabetically first ower pays the alphabetically first owee. In the
second stage of a settlement the alphabetically first remaining ower pays
the alphabetically first remaining owee.  The amount paid is the lessor
of what the ower still owes and the owee is still owed. This process
continues until either there are no more owers or no more owees. Consider
again the first data set. After the first stage the owers are D and F who
both owe 67 and the remaining owee is B (owed 200-67=133). So D pays B 67
and then B is still owed 66 which is what F pays B. After this F still
owes (1) but there are no more owees.

For the second data set the first stage produces no lines of output
because no one is owed the same amount as someone else owes.

File E.in needs to be copied to the directory with your solution. Use it
for testing using input redirection and when you submit your program the
system will run your program with System.in/stdin/cin connected to the
judge's version of the file.