import java.util.*;

public class Test {
    
    static void sort1(long m[],long p[],int n){
        for(int j =0;j<n;j++){
            for(int k =0;k<n-j-1;k++){
                    if(m[k]>m[k+1]){
                        long temp = m[k];
                        m[k] = m[k+1];
                        m[k+1] = temp;
                        temp = p[k];
                        p[k] = p[k+1];
                        p[k+1] = temp;
                    }
                    if(m[k]==m[k+1]){
                        if(p[k]>p[k+1]){
                           long temp = p[k];
                            p[k] = p[k+1];
                            p[k+1] = temp; 
                        }  
                    }
                }
           }
        }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int i =0;i<T;i++){
           int n = sc.nextInt();
           long m[] = new long[n];
           long p[] = new long[n];
           for(int j =0;j<n;j++){
               m[j] = sc.nextLong();
               p[j] = sc.nextLong();
           }
            sort1(m,p,n);
            long count_so_far =0;
            long cost_so_far =0;
            long min =1000000000;
            for(int j=0;j<n;j++){
                count_so_far =1;
                if(m[j]>0)
                cost_so_far =p[j];
                else
                cost_so_far =0;
                for(int k =0;k<n;k++){
                  if(k!=j){
                      if(m[k]>count_so_far){
                        cost_so_far +=p[k];  
                      }
                     count_so_far++; 
                  }  
                }
                if(cost_so_far < min)
                    min = cost_so_far;
            } 
            System.out.println(min);
        }
        
    }
}