#include<bits/stdc++.h>
using namespace std;
int T,a[100010],b[100010];
inline int readd()
{
char c;
bool flag=false;
while((c=getchar())>'9'||c<'0')if(c=='-')flag=true;
int res=c-'0';
while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0';
return flag ? -res : res;
}
int main()
{
T=readd();
while(T)
{
T-=1;
int n=readd();
for(int i=1;i<=n;++i)a[i]=readd(),b[i]=readd();
int x=0,y=1;
while(true)
{
if((x==0&&y==-1)||(x==n+1&&y==1))break;
if(y==1)
{
x+=1;
if(a[x-1]>0)y=-1;
a[x-1]-=1;
}
else
{
x-=1;
if(b[x-1]>0)y=1;
b[x-1]-=1;
}
}
cout<<x<<endl;
}
return 0;
}