// So if an approach seems correct and no proof dont ever go down that road, 
// as Life is short and there are many ways to fail using approaches
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional>

using namespace std;
using namespace __gnu_pbds; 

typedef tree<int, null_type, less<int>, rb_tree_tag, 
             tree_order_statistics_node_update> 
    policy_set;  

#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define ALL(c) c.begin(),c.end()
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define fr(i,a,b) for(ll i=a;i<=b;i++)

typedef long double ld;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
#define debug(...) fprintf(stdout, __VA_ARGS__), fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false); debug("%s time : %.4fs", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define cond(b,a,c) ((b)?(a):(c))
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const unordered_set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}template <class S, class T>ostream& operator <<(ostream& os, const unordered_map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}template <class T>ostream& operator <<(ostream& os, const multiset<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {ll idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...);}template <class T> void prc(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;} cerr << "]\n";}
const ll MOD=998244353;
ll mod_pow(ll x,ll p){ll ans=1;while(p){if(p&1)ans=(ans*x)%MOD;x=(x*x)%MOD;p/=2;}return ans;}

void print_array(ll* a,ll left,ll right){
	for(ll i=left;i<=right;i++)cout<<a[i]<<" ";
	cout<<endl;
}

void pre(){


}
const ll N=5002;
// vll v[N];
vpll a;
ll n,dp[N][N];
// so this dp solution can be solved after all that dp you did, in CC
// let dp[i][j] denote, for the i..nth voter min cost if I have j voters already
ll rec(ll i,ll j){
    if(i==n)return 0;
    if(dp[i][j]!=-1)return dp[i][j];
    ll ans=1e12+7;
    if(j>=a[i].ff){
        ans=min(ans,rec(i+1,j+1));
    }
    if(j+n-i-1>=a[i].ff){
        // can pick this guy for free later,
        ans=min(ans,rec(i+1,j));
    }
    // this dp fails because there are cases of skip now,
    // buy later
    // have to take all voters cant skip stuff
    ans=min(ans,a[i].ss+rec(i+1,j+1));
    return dp[i][j]=ans;
} 
void solve(){
	cin>>n;
    a.clear();
    fr(i,1,n){
        ll p,q;cin>>p>>q;
        a.pb({p,q});
    }
    // why sorting helps, as leaving people with most cost, then,
    // wrt more people, 
    sort(all(a));reverse(all(a));
    // first min m then max?, then only the drop and pick later works I think
    fr(i,0,n)fr(j,0,n)dp[i][j]=-1;
    cout<<rec(0,0)<<"\n";
}

int main() {
	// #ifndef ONLINE_JUDGE
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	// #endif
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	// time__("pre"){
		pre();
	// }
	int t=1;
	cin>>t;
	//cout<<t<<endl;
	for(int i=0;i<t;i++)
		solve();
	return 0;
}
