Secret Santa P96432


Statement
 

pdf   zip

thehtml

A group of friends decides to play to the (regrettable) “Secret Santa”, in which each one makes, in principle anonymously, a present to other person of the group. You have obtained a list of who has made the present to who, together with the prices of the presents. With this information it is easy to know the balances of the friends. Write a program that computes and prints them.

Input

Input consists of a natural n followed by an empty line and n cases, separed by an empty line. Each case consists of a natural m≥ 2 that indicates the number of friends. Follow m lines, each one with the name of who has done the present, the name of the receiver, and the price of the present (a natural). The names consist of an uppercase letter followed by one or more lowercase letters. All the name of the same case are different.

Output

For each case, print m lines, each one with the name of a friend and his balance, in increasing order by balance. In the event of a tie, it must be written before the information conrresponding to the friend with the smallest alphabetically name. Print an empty line after each case.

Public test cases
  • Input

    2
    
    2
    John Peter 1000
    Peter John 800
    
    4
    Mary John 2000
    John James 3000
    Anna Mary 1000
    James Anna 300
    

    Output

    John -200
    Peter 200
    
    John -1000
    Mary -1000
    Anna -700
    James 2700
    
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++ Python