#include using namespace std; #define Int register int #define MAXN 100005 template inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;} template inline void read (T &t,Args&... args){read (t);read (args...);} template inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');} int t,n,x[MAXN],y[MAXN],cnt[MAXN]; signed main(){ read (t); while (t --){ memset (cnt,0,sizeof (cnt)); read (n);int ans = 0; for (Int i = 1;i <= n;++ i){ read (x[i],y[i]); if (x[i] == 1) cnt[y[i]] ++; } for (Int i = 1;i <= n;++ i){ if (x[i] == 2){ int now = y[i] + (!cnt[y[i] + 1] ? 2 : 3); ans = max (ans,now); } else{ int now = y[i] + 1; ans = max (ans,now); } } write (ans),putchar ('\n'); } return 0; }