/* 做的时候手推了一下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 = 100050,N = 100050; ll read(){ ll x=0,f=1; char ch=getchar(); while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}; while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');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,1,n) s[i].a = read(),s[i].b = read(); sort(s+1,s+1+n,cmp); cal[1] = s[1].a + s[1].b; sum[1] = s[1].a; fo(i,2,n){ sum[i] = sum[i-1] + s[i].a; cal[i] = max(cal[i-1],sum[i]) + s[i].b; } cout<<cal[n]<<endl; } return 0; }