/*
ID: bbishop1
LANG: JAVA
TASK: gift1
*/

/* Program Description */
/* 
 * (1) Read the first line of gift1.in
 * (2) Create an array with that many integers.
 */
import java.io.*;
import java.util.*;

class Person {
	public String name;
	public String money; 
	public int money2;
	public int givenMoney;
	public Person[] friends;

	public Person (String monies, Person[] moreFriends, String name2) {
		// blah..
		name = name2;
		friends=moreFriends;
		money=monies;
	}
	public Person() {
		// default: no monies
		money="0";	
	}
	public void addFriend(Person w) {
		friends[friends.length+1] = w;
	}
	public void calculate() {
		money2 = (new Integer(money)).intValue();
		int moneyForEach = money2/(friends.length);
		int moneyToKeep = money2-(moneyForEach*friends.length);
		if(moneyToKeep < 0) {
			moneyToKeep = 0;
		}
		
		for(int kp = 0; kp < friends.length; kp++) {
			friends[kp].givenMoney += moneyForEach;
		}
		money2 = moneyToKeep;
		money = "";
		money += (new Integer(moneyToKeep));
	}
}
// November 30th
public class gift1 {

    public static void main(String[] args) throws IOException {
	File inputFile = new File("gift1.in");
	File outputFile = new File("gift1.out");

// create an array once the total number of people is known
// 

        FileReader in = new FileReader(inputFile);
        FileWriter out = new FileWriter(outputFile);
        
        int totalNumberOfUselessnesses = in.read();
        //int[] arr = new int[totalNumberOfUselessnesses];
     
        Person[] people = new Person[totalNumberOfUselessnesses];
	for(int kj = 0; kj<totalNumberOfUselessnesses; kj++) {
		people[kj] = new Person();	
	}
        // hm.. on second thought, people should be an array of Person objects
        // Person objects have a string for their name, one for the amount of
        // money they have, and an array of references to the people they will be giving money to
 int c;        
for(int i = 0; i <= totalNumberOfUselessnesses; i++) {
        	while((c=in.read()) != '\n' ) {
        		people[i].name+c;
        	}
        	// c=in.read(); // read the \n --> although, this should have already been done thanks to the check above  in the conditional portion of the while loop
        }
        // Should have all the people's names now

	// next step: do the following totalNumberOfUselessnesses times:
	// Read the first name. This is the person's name. Find that person in the people array. Use that index.
	// Read a newline character.
	// Read the amount of money this person has.
	// Read a space.
	// Read the number of friends this person has.
	// Read a newline character.
	// For each of those friends, read the list of friends (separated by newline characters) and find each friend's indexID and store the friend in this person's object .
	// go back to "next step"

String thisName;
int personsIndexID;
String monies;
int monies2;
String numberOfFriends = "0";
int numberOfFriends2;
String friendsName;
for(int tt = 0; tt < totalNumberOfUselessnesses; tt++) {
	thisName="";
	monies = "0";
	numberOfFriends = "0";
	friendsName = "";

	while((c=in.read()) != '\n' ) {
        		thisName+=c;
        }
	personsIndexID = findPerson(people, thisName);
	while((c=in.read()) != ' ' ) {
		monies+=c;	
	}
	// read the number of `friends`
	while((c=in.read()) != '\n') {
		numberOfFriends+=c;
	}
	numberOfFriends2 = (new Integer(numberOfFriends)).intValue();
	monies2 = (new Integer(monies)).intValue();
	for(int p = 0; p < numberOfFriends2; p++) {
		while((c=in.read()) != '\n') {
			friendsName+=c;
		}
		// add the `friend` to the list of this person's list
		people[personsIndexID].addFriend(People[findPerson(people, friendsName)]);
	}
	// "is this.. the end?"
}

// now process the data

for(int u = 0; u < totalNumberOfUselessnesses; u++) {
	People[u].calculate();
	//out.write(People[u].name + " " + (People[u]);
}

for(int u = 0; u < totalNumberOfUselessnesses; u++) {
	out.write(People[u].name + " " + (new Integer(People[u].money2)));
}

// now do the output 


/*
** Start the reference material.
        int next = 0;
int answer1 = 1;
int answer2 = 1;
        while ((c = in.read()) != -1) {
        	if(c == '\n') {
        		next = 1;
        	} else if(next == 0) {
        		answer1*=lookupValue(c);
        	} else if (next == 1) {
        		answer2*=lookupValue(c);
        	}
        }
       
           //out.write(c);

** End the reference material.
*/

        in.close();
        out.close();
        System.exit(0);
    }
	static int findPerson(Person[] x, String y) {
		int returnValue = -1;
		for(int i = 0; i < x.length; i++) {
			if(x[i].name.compareTo(y) == 0) {
				returnValue = i;
			}
		}
		return returnValue;
	}
}
