
/*
做的时候手推了一下n=2的四种情况,再排一下序就可以了,证明不是很严谨,但我想这就行了,毕竟全是套路
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define fo(i,l,r) for(int i = l;i <= r;i++)
#define fd(i,l,r) for(int i = r;i >= l;i--)
using namespace std;
const int maxn = ,N = ;
ll read(){
ll x=,f=;
char ch=getchar();
while(!(ch>=''&&ch<='')){if(ch=='-')f=-;ch=getchar();};
while(ch>=''&&ch<=''){x=x*+(ch-'');ch=getchar();};
return x*f;
}
struct sb{
ll a;
ll b;
}s[N];
int T;
int n,m;
ll cal[N],sum[N];
ll ans = 987654321234500LL;
bool cmp(sb x,sb y){
return min(x.b,y.a) > min(y.b,x.a);
}
int main(){
freopen("game.in","r",stdin);
freopen("game.out","w",stdout);
T = read();
while(T--){
n =read();
fo(i,,n) s[i].a = read(),s[i].b = read();
sort(s+,s++n,cmp);
cal[] = s[].a + s[].b;
sum[] = s[].a;
fo(i,,n){
sum[i] = sum[i-] + s[i].a;
cal[i] = max(cal[i-],sum[i]) + s[i].b;
}
cout<<cal[n]<<endl;
}
return ;
}