public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        HashMap<Integer, Balance> balanceMap = new HashMap<Integer, Balance>(); // map to cache balance
        int numBalance = Integer.parseInt(br.readLine());
        for (int i = 0; i < numBalance; i++) {
            String[] line1 = br.readLine().split(" ");
            String[] line2 = br.readLine().split(" ");
            Balance b = new Balance(Integer.parseInt(line1[0]), Integer.parseInt(line2[0]));
            for (int j = 1; j < line1.length; j++) {
                b.addToLeftBalance(Integer.parseInt(line1[j]));
            }
            for (int k = 1; k < line2.length; k++) {
                b.addToRightBalance(Integer.parseInt(line2[k]));
            }
            balanceMap.put(i, b); // add to map
        }
        // construct the structure
        for (Integer i : balanceMap.keySet()) {
            Balance b = balanceMap.get(i);
            ArrayList<Integer> leftBs = b.getLeftBalance();
            for (Integer j : leftBs) {
                b.addToLeftChild(balanceMap.get(j));
            }
            ArrayList<Integer> rightBs = b.getRightBalance();
            for (Integer j : rightBs) {
                b.addToRightChild(balanceMap.get(j));
            }
        }
        
        for (Integer i : balanceMap.keySet()) {
        	balanceMap.get(i).getWeight(); // get weight for every balance
        }
        for (int i = 0; i < numBalance; i++) {
        	System.out.println(i+": "+balanceMap.get(i).leftextra+" "+balanceMap.get(i).rightextra);
        }
    }