#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int c1 = 0, c2 = 0; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; if (x == 1) { c1 = max(c1, y); } else { c2 = max(c2, y); } } if (c2 >= c1) { cout << c2 + 2 << "\n"; } else { if (c2 == 0 || c2 < c1 - 1) { cout << c1 + 1 << "\n"; } else { cout << c1 + 2 << "\n"; } } } }