#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define mod         1000000007
#define pi          acos(-1.0)
#define eps         1e-9

#define fs          first
#define sc          second
#define pb         push_back
#define mp(a,b)     make_pair(a,b)
#define sp          printf(" ")
#define nl          '\n'
#define all(a) a.begin(),a.end()
#define itn int

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))


#define pr1(x)      cout<<x<<"\n"
#define pr2(x,y)    cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)  cout<<x<<" "<<y<<" "<<z<<"\n"


typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<LL> vll;
typedef vector<double> vd;

// clock
typedef long double f80;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
//

int dp[5005][5005];
vector<LL> m,p;
vector<int> order;
int n;

bool custom(int a, int b)
{
    return m[a]>m[b];
}
LL solve(int in, int tn)
{
    if(in==n)
         return 100000000;
    if(tn>=m[order[in]])
        return dp[in][tn]=0;
    if(dp[in][tn]!=-1)
        return dp[in][tn];
    return dp[in][tn]=min(p[order[in]]+solve(in+1,tn+1),solve(in+1,tn));
}

int main()
{
    ios::sync_with_stdio(0); 
    cin.tie(0);
    cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    int t;
    cin>>t;
    while(t--)
    {
        m.clear();
        p.clear();
        order.clear();
        cin>>n;
        m.resize(n);
        p.resize(n);
        order.resize(n);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                dp[i][j]=-1;
            order[i]=i;
        }
        for(int i=0;i<n;i++)
            cin>>m[i]>>p[i];
        sort(all(order),custom);
        cout<<solve(0,0)<<nl;
    }


    #ifdef rd
    cout<<endl<<endl<<"Time elapsed: "<<(f80)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif

}
