1 #include <cstdio>
2 #include <cmath>
3 #include <iostream>
4 using namespace std;
5 int M, N;
6 int T[1005][1005], t, s[1005]/*第i个点的扇区*/;
7 int sD(int a,int b)
8 {
9 int x = abs(s[a]-s[b]);
10 if(x > 180)x = 360-x;
11 return x;
12 }
13 void deal()
14 {
15 int i,j;
16 scanf("%d", &N);
17 T[0][0] = s[0] = 0; //T[i][j]中i>j,表示从(0,0)分别到i,j的最短路径长度,s[i]表示i点的扇区
18 for(i = 1;i <=N;++i)
19 {
20 scanf("%d%d",&t,s+i);
21 T[i][0] = T[i-1][0] + sD(i, i-1);
22 }
23 for(i = 2;i <=N;++i)
24 {
25 for(j = 1;j <i-1;++j)
26 T[i][j] = T[i-1][j] + sD(i-1,i);
27 int min = 0x7FFFFFFF;
28 for(j = 0;j<i-1;++j) //j的意义已改变,"去路端点"为点i-1
29 if(min > T[i-1][j] + sD(j, i))min = T[i-1][j] + sD(j, i);
30 T[i][i-1] = min;
31 }
32 printf("%d\n", T[N][N-1] + sD(N, N-1) + t * 800 + 10 * N);
33 }
34 int main()
35 {
36 scanf("%d", &M);
37 for(int i = 0;i<M;++i)
38 deal();
39 return 0;
40 }