#include using namespace std; int main() { int t; cin.tie(0); ios_base::sync_with_stdio(false); cin >> t; while (t--) { int n; cin >> n; int x, y; int l = 0, r = 0; for (int i = 0; i < n; i++) { cin >> x >> y; if (x == 1) { r = max(r, y); } else { l = max(l, y); } } if (n == 1) { if (l) cout << (l-1)+3 << endl; else cout << (r-1)+2 << endl; continue; } int res = 0; if (l >= r) { res = (l-1)+3; } else if (r-l == 1) { res = (l-1)+4; } else if (r-l >= 2) { res = (r-1)+2; } cout << res << endl; } }