#include #include int n; void solve(){ scanf("%d",&n); int mx[2]={0}; for(int i=1,id,p;i<=n;i++){ scanf("%d%d",&id,&p); mx[id-1] = std::max(mx[id-1],p); } if(mx[1]==0){ printf("%d\n",mx[0]+1); return; } if(mx[0]==0){ printf("%d\n",mx[1]+2); return; } if(mx[1]>=mx[0]) printf("%d\n",mx[1]+2); else printf("%d\n",std::max(mx[0]+1,mx[1]+3)); } int main(){ int T; scanf("%d",&T); while(T--) solve(); }