#include using namespace std; typedef long long ll; typedef double db; typedef pair pii; #define fir first #define sec second #define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i) #define rrp(i,a,b) for (int i = (a) ; i >= (b) ; -- i) #define gc() getchar() template inline void read(tp& x) { x = 0; char tmp; bool key = 0; for (tmp = gc() ; !isdigit(tmp) ; tmp = gc()) key = (tmp == '-'); for ( ; isdigit(tmp) ; tmp = gc()) x = (x << 3) + (x << 1) + (tmp ^ '0'); if (key) x = -x; } template inline void ckmn(tp& x,tp y) { x = x < y ? x : y; } template inline void ckmx(tp& x,tp y) { x = x < y ? y : x; } const int N = 100010; int mat[2][N], n; void solve() { int x,y; read(n); memset(mat, 0, sizeof mat); int ans = 0; rep (i, 1, n) { read(x), read(y); mat[x-1][y] = 1; if (x == 1) ans = max(ans, y + 1); else ans = max(ans, y + 2); } rep (i, 1, 100000) { if (mat[1][i] && mat[0][i+1]) { ans = max(ans, i+3); } } printf("%d\n", ans); } int main() { int T; read(T); while (T --) solve(); return 0; }