#include <bits/stdc++.h>
#include <direct.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
#define M(x) \
{ \
x %= mod; \
if (x < 0) \
x += mod; \
}
#define error \
{ \
cout << "No\n"; \
return; \
}
#define all(x) (x.begin(), x.end())
const int N = 1e6 + 10;
int a[N];
int n, m;
int l[N], r[N];
bool tui[N];
void solve()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> l[i] >> r[i];
tui[i] = 0;
}
int ans = 0;
int msk = 0;
for (int i = 29; i >= 0; i--)
{
msk += 1 << i;
int f = 1;
for (int j = 1; j <= n; j++)
{
if (f == 0)
break;
if (!(r[j] & (1 << i)))
f = 0;
}
if (f == 1)
{
ans += 1 << i;
}
for (int j = 1; j <= n; j++)
{
if ((msk & r[j]) > ans && ((r[j] & msk) - 1 >= l[j]) && !tui[j])
{
r[j] = (r[j] & msk) - 1;
tui[j] = 1;
} else if((msk&r[j])>ans&&!tui[j]) {
r[j]=r[j]-(1<<i);
}
}
}
cout << ans << "\n";
}
void print(int x)
{
for (int i = 29; i >= 0; i--)
{
cout << ((x >> i) & 1);
}
cout << "\n";
}
signed main()
{
// freopen("D://Desktop//VSCODE//input.txt", "r", stdin);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
// 29882460 33554431
// print(29882460);
// print(93281727);
while (t--)
{
solve();
}
}