#include #include inline int Max(const int &x, const int &y) { return x > y ? x : y; } inline int input() { static int p; scanf("%d", &p); return p; } // inline double inputf() { // static double p; // scanf("%lf", &p); // return p; // } int main() { int mx[3]; for (int T = input(); T; --T) { const int n = input(); mx[1] = mx[2] = 0; for (int i = 1; i <= n; ++i) { const int x = input(), y = input(); mx[x] = Max(mx[x], y); } if (mx[2] + 2 <= mx[1] or mx[2] == 0) printf("%d\n", 1 + mx[1]); else if (mx[2] >= mx[1]) printf("%d\n", 2 + mx[2]); else printf("%d\n", 2 + mx[1]); } return 0; }