#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef string str;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define forr(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i)
#define ford(i, r, l) for (int i = (int)(r); i >= (int)(l); --i)
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
const ll INF = 2e15; // Ngưỡng chặn trên để ngăn tràn số hệ nhị phân
bool check(ll T, const vector<pll>& elements) {
ll surplus = 0;
vector<pll> under_T;
// Tách tài nguyên: các số >= T tự động hóa thành 0
for (const auto& p : elements) {
if (p.fi >= T) {
surplus += p.se;
} else {
under_T.pb(p);
}
}
ll R = 1; // Yêu cầu ban đầu tại đích T
ll curr = T;
for (const auto& p : under_T) {
ll u = p.fi;
ll cnt = p.se;
ll L = curr - 1 - u; // Số lượng tầng trống bị bỏ qua giữa các số
if (L > 0) {
if (R > 0 && L >= 60) R = INF;
else {
if (R > 0 && (INF / (1LL << L) < R)) R = INF;
else R = R * (1LL << L);
}
}
if (u > 0) {
if (cnt >= R) {
surplus += (cnt - R); // Lượng dư thừa đẩy về làm tài nguyên số 0
R = 0;
} else {
R = 2 * R - cnt; // Lan truyền nhân đôi lượng thiếu hụt xuống dưới
}
curr = u;
} else { // Khi chạm tới u == 0
ll extra = (R > cnt) ? (R - cnt) : 0LL;
return surplus >= extra;
}
if (R > INF) R = INF;
}
// Xử lý khoảng trống cuối cùng nếu danh sách chưa chạm đến số 0
ll L = curr;
if (L > 0) {
if (R > 0 && L >= 60) R = INF;
else {
if (R > 0 && (INF / (1LL << L) < R)) R = INF;
else R = R * (1LL << L);
}
}
return surplus >= R;
}
void solve() {
int n;
if (!(cin >> n)) return;
vector<pll> elements(n);
ll max_x = 0;
forn(i, n) {
cin >> elements[i].fi >> elements[i].se;
cmax(max_x, elements[i].fi);
}
// Sắp xếp các phần tử giảm dần theo giá trị x_i
sort(all(elements), [](const pll& a, const pll& b) {
return a.fi > b.fi;
});
// Phạm vi tìm kiếm tối ưu quanh max_x do tính chất tăng lũy thừa của yêu cầu trống
ll low = max_x, high = max_x + 65, ans = max_x;
while (low <= high) {
ll mid = low + (high - low) / 2;
if (check(mid, elements)) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
if (cin >> t) {
while (t--) {
solve();
}
}
return 0;
}