#include #define ls rt<<1 #define rs rt<<1|1 using namespace std; typedef unsigned long long ll; const ll inf = (1ll << 63); const int maxn = 1000001; int d[3][101000]; int main() { int t; scanf("%d", &t); while(t--) { int n; scanf("%d", &n); for(int i = 1; i <= 100000; i++) d[1][i] = d[2][i] = 0; for(int i = 1; i <= n; i++) { int x, y; scanf("%d%d", &x, &y); d[x][y] = 1; } int ans = 0; for(int i = 100000; i >= 1; i--) { if(d[1][i] || d[2][i]) { if(d[1][i] == 1) { if(d[1][i - 1]) ans = i + 2; else ans = i + 1; } else ans = i + 2; break; } } cout << ans << endl; } return 0; }