Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.

The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.

Suppose Ciel and Jiro play optimally, what is the score of the game?

Input

The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.

Output

Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.

Examples

Input

2
1 100
2 1 10


Output

101 10


Input

1
9 2 8 6 5 9 4 7 1 3


Output

30 15


Input

3
3 1 3 2
3 5 4 6
2 8 7


Output

18 18


Input

3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000


Output

7000 7000

Note

In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.

In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.*;

/**
 * @author Artem_Mikhalevitch
 */
public class Task extends ASolver {

    List<Integer> list = new LinkedList<Integer>();
    long ceil = 0, jira = 0;
    int n;

    public static void main(String[] args) throws ParseException {

        Task task = new Task();
        task.init();
        if (task.readData()) {
            //task.solve();
            task.writeData("" + task.solve());
        }
    }

    @Override
    public void init() {
        super.init();
    }

    @Override
    public boolean readData() {
        try {
            n = nextInt();
            for (int i = 0; i < n; i++) {
                int m = nextInt();
                int p = m / 2;
                if(m%2==0){
                    p--;
                }
                for (int j = 0; j < p; j++) {
                    ceil += nextInt();
                }
                if (m % 2 == 1) {
                    list.add(nextInt());
                }else{
                    ceil+=nextInt();
                }
                for (int j = p+1; j < m; j++) {
                    jira += nextInt();
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.getCause());
            System.out.println(ex.getMessage());
            return false;
        }
        return true;
    }


    public String solve() {
        Collections.sort(list);
        Collections.reverse(list);
        int k = 0;
        for (int i : list) {
            if (k % 2 == 0) {
                ceil += i;
            } else {
                jira += i;

            }
            k++;
        }
        return "" + ceil + " " +jira ;
    }
}

abstract class ASolver {

    protected StringTokenizer tokens;
    protected BufferedReader input;
    protected PrintWriter output;

    public void init() {
        input = new BufferedReader(new InputStreamReader(System.in));
    }

    public abstract boolean readData();

    public void writeData(String result) {
        System.out.println(result);
    }

    public void writeData(int result) {
        System.out.println(result);
    }

    public void writeData(long result) {
        System.out.println(result);
    }

    public int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    public long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public String next() throws IOException {
        while (tokens == null || !tokens.hasMoreTokens()) {
            tokens = new StringTokenizer(input.readLine());
        }
        return tokens.nextToken();
    }

    public String nextLine() throws IOException {
        while (tokens == null || !tokens.hasMoreTokens()) {
            tokens = new StringTokenizer(input.readLine(), "\n");
        }
        return tokens.nextToken();
    }

    public long infinityImitator(long a, long b) {
        if (a == Long.MIN_VALUE) {
            return Long.MIN_VALUE;
        } else {
            return a + b;
        }
    }

    public int infinityImitator(int a, int b) {
        if (a == Integer.MIN_VALUE) {
            return Integer.MIN_VALUE;
        } else {
            return a + b;
        }
    }

    /*
     * public long max(long... a) { long result = Long.MIN_VALUE; for (int i =
     * 0; i < a.length; i++) { result = Math.max(result, a[i]); } return result;
     * }
     */
    public int indexMax(int... a) {
        int result = 0;
        int index = -1;
        int length = a.length;
        for (int i = 0; i < length; i++) {
            if (a[i] > result) {
                result = a[i];
                index = i;
            }
        }
        return index;
    }

    public int indexMax(long... a) {
        long result = 0;
        int index = -1;
        int length = a.length;
        for (int i = 0; i < length; i++) {
            if (a[i] > result) {
                result = a[i];
                index = i;
            }
        }
        return index;
    }

    public int max(int... a) {
        int result = Integer.MIN_VALUE;
        for (int i = 0; i < a.length; i++) {
            result = Math.max(result, a[i]);
        }
        return result;
    }

    public long max(long... a) {
        long result = Integer.MIN_VALUE;
        for (int i = 0; i < a.length; i++) {
            result = Math.max(result, a[i]);
        }
        return result;
    }
}

class ArraySorter {

    public static int[] sortIntArray(int[] array) {

        int length = array.length;
        if (length == 1) {
            return array;
        } else if (length == 2) {
            return array[0] < array[1] ? array : new int[]{array[1], array[0]};
        } else {
            int l1 = length / 2, l2 = length - l1;
            int[] array1 = new int[l1];
            int[] array2 = new int[l2];
            System.arraycopy(array, 0, array1, 0, l1);
            System.arraycopy(array, l1, array2, 0, l2);
            array1 = sortIntArray(array1);
            array2 = sortIntArray(array2);
            return splitSortedIntArrays(array1, array2);
        }
    }


    private static int[] splitSortedIntArrays(int[] mas1, int[] mas2) {
        int l1 = mas1.length;
        int l2 = mas2.length;
        int[] result = new int[l1 + l2];
        int i1 = 0, i2 = 0, i;
        for (i = 0; i1 < l1 && i2 < l2; i++) {
            if (mas1[i1] <= mas2[i2]) {
                result[i] = mas1[i1++];
            } else {
                result[i] = mas2[i2++];
            }
        }
        for (int j = i; i1 < l1; j++) {
            result[j] = mas1[i1++];
        }
        for (int j = i; i2 < l2; j++) {
            result[j] = mas2[i2++];
        }
        return result;
    }

    private static double[] splitSortedDoubleArrays(double[] mas1, double[] mas2) {
        int l1 = mas1.length;
        int l2 = mas2.length;
        double[] result = new double[l1 + l2];
        int i1 = 0, i2 = 0;
        int i = 0;
        for (i = 0; i1 < l1 && i2 < l2; i++) {
            if (mas1[i1] <= mas2[i2]) {
                result[i] = mas1[i1++];
            } else {
                result[i] = mas2[i2++];
            }
        }
        for (int j = i; i1 < l1; j++) {
            result[j] = mas1[i1++];
        }
        for (int j = i; i2 < l2; j++) {
            result[j] = mas2[i2++];
        }
        return result;
    }
}
/*class ABSComparator implements Comparator<Pair> {

    /**
     * Compares its two arguments for order. Returns a negative integer, zero,
     * or a positive integer as the first argument is less than, equal to, or
     * greater than the second.
     */
/*@Override
public int compare(Pair o1, Pair o2) {
   if (o1.getA() == o2.getA()) {
       return o2.getB() - o1.getB();
   }
   return o1.getA() - o2.getA();
}
}     */