#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inf 9223372036854775807
#define mini 9223372036854775807
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
pair<ll,ll>s4[4]={{-1,0},{1,0},{0,-1},{0,1}};
pair<ll,ll>s8[8]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,1},{1,0},{1,-1}};
ll power(ll a,ll b)
{
	if(b==0)
		return 1;
	ll c=power(a,b/2);
	if(b%2==0)
		return ((c%mod)*(c%mod))%mod;
	else
		return ((((c%mod)*(c%mod))%mod)*a)%mod;
}
int main()
{
	boost
	ll t,i;
	cin>>t;
	for(i=0; i<t; i++)
	{
		ll n;
		cin>>n;
		ll dp[n+5][n+5];
		memset(dp,0,sizeof(dp));
		ll m[n],p[n];
		for(ll j=0; j<n; j++)
			cin>>m[j]>>p[j];
		if(n==1)
		{
			if(m[0]==0)
				cout<<"0"<<endl;
			else
				cout<<p[0]<<endl;
			continue;
		}
		ll minu=0;
		ll sminu=0;
		for(ll j=1; j<=n; j++)
		{
			ll t_minu=mini;
			ll t_sminu=mini;
			for(ll k=0; k<n; k++)
			{
				if(j>=(m[k]+1))
				{
					if(dp[k][j-1]==minu)
						dp[k][j]=sminu;
					else
						dp[k][j]=minu;
				}
				else
				{
					if(dp[k][j-1]==minu)
						dp[k][j]=sminu+p[k];
					else
						dp[k][j]=minu+p[k];
				}
				if(dp[k][j]<=t_minu)
				{
					t_sminu=t_minu;
					t_minu=dp[k][j];
				}
				else if(dp[k][j]<=t_sminu)
					t_sminu=dp[k][j];
			}
			minu=t_minu;
			sminu=t_sminu;
		}
		ll ans=mini;
		for(ll j=0; j<n; j++)
			ans=min(ans,dp[j][n]);
		cout<<ans<<endl;
	}
	return 0;
}