1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 typedef long long ll; 5 const int N=1e6+10; 6 ll a[N],b[N]; 7 int n; 8 int main() 9 { 10 int T; 11 scanf("%d",&T); 12 while(T--) 13 { 14 scanf("%d",&n); 15 ll sb=0,ans=0; 16 for(int i=1;i<=n;i++) 17 { 18 scanf("%lld%lld",&a[i],&b[i]); 19 sb+=b[i]; 20 } 21 for(int i=1;i<=n;i++) 22 { 23 ll t=max(b[i]-ans,ll(0)); 24 ll z=min(sb-t,a[i]); 25 ans+=z; 26 sb-=z; 27 } 28 printf("%lld\n",ans); 29 } 30 return 0; 31 }