#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
const int B=500;
using namespace std;
using namespace __gnu_pbds;
#define be begin()
#define rb rbegin()
#define all(v) v.begin(),v.end() 
#define rep(i,start,lim) for(long long (i)=(start);i<(lim);i++)
#define nod int node,int l,int r
#define lson node*2,l,(l+r)/2
#define rson node*2+1,(l+r)/2+1,r
typedef long long ll;
typedef long double D;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> rbTree;
const ll inf= 1e9+10;
ll MOD=  1e9+7 ;
const ll mxn= 5e3+10;
const int N=10000005;
const double pi=3.14159;
#define mp make_pair
#define mp3(a,b,c) mp(a,mp(b,c))
typedef pair<int,int> pii;
typedef pair<int,pair<int,int> > piii;
typedef pair<int,pair<int,pair<int,int>>> piiii;
typedef pair<ll, int> pli;
typedef pair<ll,ll> pll;
int dx[]={0,1,0,-1,1,-1,1,-1};
int dy[]={1,0,-1,0,-1,1,1,-1};
ll n,m,k,y,z,x,q;
pii a[mxn];
int dp[mxn][mxn];   //min cost to have 'n' voters voting [0], when 'k' are already voting for us.
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin>>q;
    while(q--){
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i].xx>>a[i].yy;
        sort(a+1,a+n+1);
        for(int i=1;i<=n;i++){
            for(int j=0;j<=n;j++){
                if(j>=a[i].xx) dp[i][j]=0;
                dp[i][j]= dp[i-1][j+1]+a[i].yy;
                if(a[i].xx<=i+j-1) dp[i][j]=min(dp[i][j],dp[i-1][j]);
            }
        }
        cout<<dp[n][0]<<endl;
    }
    
}
        
   