#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#define LOCAL #define ll long long #define maxn 100010 #define maxw 1000005 #define INF 1000000000000000000 using namespace std; ll n; int main() { #ifdef LOCAL freopen("data.in.txt", "r", stdin); freopen("data.out.txt", "w", stdout); #endif int t; cin >> t; while(t--) { scanf("%lld", &n); ll maxy[2] = {0, 0}; for(int i = 0; i < n; i++) { int x; ll y; scanf("%d%lld", &x, &y); maxy[x-1] = max(maxy[x-1], y); } ll ans = 0; if(maxy[0] == 0) ans = maxy[1]+2; else if(maxy[1] == 0) ans = maxy[0]+1; else if(maxy[0] > maxy[1]) ans = max(maxy[0]+1, maxy[1]+3); else ans = maxy[1]+2; cout << ans << endl; } return 0; }