import java.util.Scanner;
public class Woer {
public static int[] ceyue;
public static int guannum;
public static int rennum[];
public static int minfree=0x7fffffff;
public static int min[];
public static int qiannum[];
public static int feiyong(){
int flag0=0;
int flag1=0;
for(int i=0;i<guannum;i++){
if(ceyue[i]==1) flag0=1;
if(ceyue[i]==2) flag1=1;
}
if(flag0==0&&flag1==1) return 0x7fffffff;
flag0=0;
flag1=0;
for(int i=0;i<guannum;i++){
if(ceyue[i]==1) flag0=1;
if(ceyue[i]==2) flag1=1;
}
if(flag0==1&&flag1==0) return 0x7fffffff;
int a3=0;
int a2=0;
int a1=0;
int feiyon=0;
for(int i=0;i<guannum;i++){
if(ceyue[i]==0) {
feiyon+=qiannum[i];
}else if(ceyue[i]==1){
feiyon+=qiannum[i]*2;
a3=a3+rennum[i];
}else{
if(a1+a2+a3<rennum[i]) return 0x7fffffff;
if(a1+a2+a3>=rennum[i]){
if(a1>=rennum[i]){
a1=a2;
a2=a3;
a3=0;
}else if(a1+a2>=rennum[i]){
a1=a1+a2-rennum[i];
a2=a3;
a3=0;
}else{
a1=0;
a2=a1+a2+a3-rennum[i];
a3=0;
}
}
}
}
return feiyon;
}
public static void dFS(int layer,int endLayer){
if(layer==endLayer){
int freetemp=feiyong();
if(minfree>freetemp) {
for(int y=0;y<guannum;y++){
min[y]=ceyue[y];
}
minfree=freetemp;
}
return;
}
for(int i=0;i<3;i++){
if(layer==0&&i==2) continue;
ceyue[layer]=i;
dFS(layer+1,endLayer);
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int casenum=sc.nextInt();
while(casenum-->0){
guannum=sc.nextInt();
ceyue=new int[guannum];
rennum=new int[guannum];
qiannum=new int[guannum];
min=new int[guannum];
for(int u=0;u<guannum;u++){
rennum[u]=sc.nextInt();
qiannum[u]=sc.nextInt();
}
dFS(0,guannum);
System.out.println(minfree);
}
}
}