#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct node{int x,i;};
int a[N];
int ans,n;
vector<node>P[N],B[N];
vector<node>awa,awb;
__inline void judge(int&s,const node&k,vector<node>&A){((s&k.x)^s)?(s&=k.x,A.push_back(k)):0;}
__inline void judge(int&s,const node&k,node a[],int&T){((s&k.x)^s)?(s&=k.x,a[T++]=k):0;}
__inline void ckmax(int&A,int B){(A<B)?(A=B):0;}
__inline void TRY(int l,int r)
{
static node pr[35],nx[35];int tr=0,tx=0;
if(l>r)swap(l,r);int s=~0u>>1;
for(auto k:B[1])if(k.i>=l)break;else judge(s,k,pr,tr);
judge(s,node{a[r],l},pr,tr);
for(auto k:B[l+1])if(k.i>=r)break;else judge(s,k,pr,tr);
judge(s,node{a[l],r},pr,tr);
for(auto k:B[r+1])judge(s,k,pr,tr);
s=~0u>>1;
for(auto k:P[n])if(k.i<=r)break;else judge(s,k,nx,tx);
judge(s,node{a[l],r},nx,tx);
for(auto k:P[r-1])if(k.i<=l)break;else judge(s,k,nx,tx);
judge(s,node{a[r],l},nx,tx);
for(auto k:P[l-1])judge(s,k,nx,tx);
int p=tx-1;
for(int i=1;i<tr;++i)pr[i].x&=pr[i-1].x;
for(int i=1;i<tx;++i)nx[i].x&=nx[i-1].x;
pr[tr++]=node{0,n+1};
for(int i=0;i+1<tr;++i)
{
while(p!=-1&&nx[p].i<pr[i+1].i)--p;
if(p!=-1)ckmax(ans,pr[i].x+nx[p].x);
else break;
}
}
int sum[N],via[N];
int main()
{
int T;
cin.tie(0)->sync_with_stdio(0);
cin>>T;while(T--)
{
cin>>n;for(int i=1;i<=n;++i)cin>>a[i];
awa.clear(),awb.clear();
for(int i=1;i<=n;++i)
{
awb.push_back(node{a[i],i});int x=a[i];
for(auto k:awa)judge(x,k,awb);
P[i]=awa=awb,awb.clear();
}
awa.clear();
for(int i=n;i;--i)
{
awb.push_back(node{a[i],i});int x=a[i];
for(auto k:awa)judge(x,k,awb);
B[i]=awa=awb,awb.clear();
}
ans=0;sum[1]=a[1];
for(int i=2;i<=n;++i)sum[i]=sum[i-1]&a[i];
int S=a[n];
for(int i=n;i>1;--i)S&=a[i],ckmax(ans,S+sum[i-1]);
for(auto k:P[n])for(int i=via[k.i]=1;i<=n;++i)if(k.i!=i)TRY(k.i,i);
for(auto k:B[1])if(!via[k.i])for(int i=via[k.i]=1;i<=n;++i)if(k.i!=i)TRY(k.i,i);
for(int i=1;i<=n;++i)P[i].clear(),B[i].clear(),via[i]=0;
cout<<ans<<"\n";
}
}