Problem E: Situp Benches

Joe Six-Pack

The gym at the University of Alberta has two identical sit up benches that are side by side. Each of these benches can be inclined in 10 degree increments between 10 degrees and 50 degrees inclusive depending on the level of difficulty the user desires. At the end of each day the gym staff adjust the incline of each machine to be 10 degrees. This means at the start of each day both machines have a 10 degree incline.

Every time someone uses a bench it incurs some wear and tear that amounts to 15 cents in maintenance costs. Additionally, every time a bench has its incline changed it incurs wear and tear that amounts to the number of degrees the bench was adjusted, in cents. For example, if someone moves the bench from a 40 degree incline to a 20 degree incline then 20 cents of maintenance must be done.

The gym staff have recently required students to sign up for the situp benches the day before. When a student signs up they give the time at which they would like to use the machine and the incline they will use it at. No more than two students can sign up to use a bench at the same time.

Prior to opening the gym for the day, the gym staff would like to have a list assigning students to each of the situp benches and they would like the assignment to minimize the total maintenance cost for the day. They have asked you to write a program to make this list.

Input begins with the number of test cases on its own line. Each test case begins with the number n (1 ≤ n ≤ 10000), the number of students who have signed up to use the benches on the following day. Following are n lines, each consisting of two numbers: time_slot and incline. time_slot is a number that specifies when a student will arrive at the machine, while incline specifies the angle to which they will adjust the situp bench. Two students in the same time_slot arrive at the benches at the same time, and lower time_slots occur before higher time_slots. time_slots are numbered between 1 and 20000, and incline is one of 10, 20, 30, 40 or 50 depending on the student's preference.

For each test case, output a line containing the total maintenance cost (in cents) for the day.

Sample Input

1
3
2 40
2 50
1 40

Sample Output

185

Kevin Waugh