#include<bits/stdc++.h>
using namespace std;
int a[100005],b[100005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int x=0,y=1;
memset(a,sizeof(a),0);
memset(b,sizeof(b),0);
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i];
}
a[0]=0;
a[n+1]=0;
b[0]=0;
b[n+1]=0;
while(true){
if(x==0&&y==-1) break;
if(x==n+1&&y==1) break;
if(y==1){
x++;
if(a[x]>0) y=-1;
a[x]--;
}
else if(y==-1){
x--;
if(b[x]>0) y=1;
b[x]--;
}
}
cout<<x<<'\n';
}
return 0;
}