#include #include #include #include #include using namespace std; int n; int a,b1,b2; int main() { int T,x,y; scanf("%d",&T); while(T--){ a = b1 = b2 = -1; scanf("%d",&n); for( int i = 1; i <= n; i++ ) { scanf("%d%d",&x,&y); if( x == 1 ){ a = max( a , y ); } else{ if ( y > b1 ) b2 = b1 , b1 = y; else b2 = max( b2 , y ); } } if ( a == -1 ) printf("%d\n",b1+2); else if ( b1 == -1 ) printf("%d\n",a+1); else{ if ( a == b1 + 1 ) printf("%d\n",b1+3); else printf("%d\n",max(b1+2 , a+1) ); } } return 0; }