|시간 제한||메모리 제한||제출||정답||맞은 사람||정답 비율|
|1 초||256 MB||689||96||45||13.678%|
You correctly think that owning a car can cost a lot of money and can be a great trouble due to congestion. The public transportation system of your city is made up of buses and trains. The fare rule is as follows.
You thought that the rule was too complicated and you did not know how many times you would ride buses or trains. Instead of buying bus card or travel card, you paid single ride fare each time. Now you think that if you bought travel cards, then you would have spent less money.
For example, suppose your travel log for three days is as follows.
|Day||Number of bus rides||Number of train rides|
If you do not buy any travel card, you will spend \$10 in total as you spend \$1 on day 1, \$5 on day 2, and \$4 on day 3. However, you can spend \$1 on day 1, \$3 on day 2 by buying a daily bus card, and \$4 on day 3. In total, you will spend \$8, which is smaller than \$10 and optimal.
Given the travel logs of n days, write a program which calculates the minimum amount of fare.
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing one integers n (1 ≤ n ≤ 10,000), where n is the number of days. Each of the following n lines contains two integers a and b where a is the number of bus rides and b is that of train rides on that day (0 ≤ a, b ≤ 100,000). There is a single space between two integers in the same line.
Your program is to write to standard output. Print exactly one line for each test case. The line should contain the minimum amount of fare.
2 3 1 0 5 0 0 2 6 2 3 4 2 1 5 2 2 3 4 5 5