#include <iostream>
#include <cmath>
#include <vector>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <utility>
#include <cstring>
using namespace std;
#define ll long long
typedef vector<int> vi;
#define all(v) ((v).begin()),((v).end())
void fast();
long long power(ll n, ll k){
    if(k==0) return 1;
    ll sq=power(n,k/2);
    sq=sq*sq;
    if(k%2==1)
        sq=sq*n;
    return sq;
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
// freopen("input.txt" ,"r",stdin );
// freopen("output.txt","w",stdout);
vector<int> v;
map<int,int> mp;
int main()
{
    fast();
    int n,x; cin>>n;
    for(int i=0;i<n;i++) cin>>x,v.push_back(x),mp[x]++;
    sort(v.begin(),v.end());
    ll count=0;
    for(int i=1;i<=31;i++){
        ll p=power(2,i);
        for(int j=0;j<(int)v.size();j++){
            int k=int(p-v[j]);
            //cout<<k<<" ";
            int start=0,ende=n-1;
            int ans=-1;
            while(start<=ende){
                int mid=(start+ende)/2;
                if(v[mid]>k) ende=mid-1;
                else if(v[mid]<k) start=mid+1;
                else {ans=mid; break;}
            }
            if(ans!=-1){
            if(ans<n && ans!=j && v[ans]!=v[j]){
                if(mp[v[ans]]>0&&j<ans)
                    count+=mp[v[ans]];
            }
            else if(ans<n && j<ans&& v[ans]==v[j]){
                int mo=mp[v[ans]];
                count+=mo*(mo-1)/2;
                mp[v[ans]]=1;
            }
            }
        }
    }
    cout<<count;
    return 0;
}
void fast()
{std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}


#include <iostream>
#include <cmath>
#include <vector>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <utility>
#include <cstring>
using namespace std;
#define ll long long
typedef vector<int> vi;
#define all(v) ((v).begin()),((v).end())
void fast();
long long power(ll n, ll k){
    long long power=1;
    for(int i=0;i<k;i++){
        power=power*n;
    }
    return power;
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
// freopen("input.txt" ,"r",stdin );
// freopen("output.txt","w",stdout);
ll arr[100000+50];
int main()
{
    fast();
    ll n,k; cin>>n;
    for(int i=0;i<n;i++) cin>>arr[i];
    sort(arr,arr+n);
    ll cnt=0;
    for(int i=0;i<n;i++){
        for(int j=1;j<=38;j++){
            ll x=power(2,j)-arr[i];
            if(binary_search(arr+i+1,arr+n,x)){
                cnt++;break;
            }
        }
    }
    cout<<cnt;
    return 0;
}
void fast()
{std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}


#include <iostream>
#include <cmath>
#include <vector>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <utility>
#include <cstring>
using namespace std;
#define ll long long
typedef vector<int> vi;
#define all(v) ((v).begin()),((v).end())
void fast();
long long power(ll n, ll k){
    if(k==0) return 1;
    ll sq=power(n,k/2);
    sq=sq*sq;
    if(k%2==1)
        sq=sq*n;
    return sq;
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
// freopen("input.txt" ,"r",stdin );
// freopen("output.txt","w",stdout);
vector<int> v;
map<int,int> mp;
int main()
{
    fast();
    int n,x; cin>>n;
    for(int i=0;i<n;i++) cin>>x,v.push_back(x),mp[x]++;
    sort(v.begin(),v.end());
    ll count=0;
    for(int i=1;i<=31;i++){
        ll p=power(2,i);
        for(int j=0;j<(int)v.size();j++){
            int k=int(p-v[j]);
            //cout<<k<<" ";
            int start=0,ende=n-1;
            int ans=-1;
            while(start<=ende){
                int mid=(start+ende)/2;
                if(v[mid]>k) ende=mid-1;
                else if(v[mid]<k) start=mid+1;
                else {ans=mid; break;}
            }
            if(ans!=-1){
            if(ans<n && ans!=j && v[ans]!=v[j]){
                if(mp[v[ans]]>0&&j<ans)
                    count+=mp[v[ans]];
            }
            else if(ans<n && j<ans&& v[ans]==v[j]){
                int mo=mp[v[ans]];
                count+=--mo;
                --mp[v[ans]];
            }
            }
        }
    }
    cout<<count;
    return 0;
}
void fast()
{std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}


#define N 5005
#define pi 3.1415926535897932384626
#define ee 2.71828182
#define inf 0x3f3f3f3f
#define mod 1000007
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<string>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,m,s[N];
int k[N][N];
int kk[N][N];
int dfs(int l,int r)
{
    if(l==r)
    {
        return kk[l][r]=k[l][r];
    }
    if(kk[l][r]!=-1)return kk[l][r];
    int a,b;
    if(kk[l][r-1]==-1)
        a=dfs(l,r-1);
    else a=kk[l][r-1];
    if(kk[l+1][r]==-1)
        b=dfs(l+1,r);
    else b=kk[l+1][r];
    kk[l][r]=a>b?a:b;
    k[l][r]=k[l][r-1]^k[l+1][r];
    kk[l][r]=kk[l][r]>k[l][r]?kk[l][r]:k[l][r];
    return kk[l][r];
}
int main()
{
    memset(kk,-1,sizeof(kk));
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d",&k[i][i]);
    scanf("%d",&m);
    int l,r;
    for(int i=0; i<m; i++)
    {
        scanf("%d%d",&l,&r);
        printf("%d\n",dfs(l,r));
    }
    return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define dracarys ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("avx2")

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define dbg(x) cout << "[ " << x << " ] "
#define ump unordered_map

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash{
    int operator()(int x) const { return x ^ RANDOM; }
};

typedef vector<vector<vector<vector<int>>>>  vvvvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;

const int mod = 1e9 + 7;
const int mxN = 5e2 + 5;
int val[mxN];
vvvvi rmq;

int inline max(const int x,const int y){return (x > y ? x : y);}
int inline max(const std::vector<int> v){
    int mx = v[0];
    for (const int& x : v){
        mx = (x > mx ? x : mx);
    }
    return mx;
}
int n;
int LOGN;
void pre(){
    for (int szr = 1;szr <= LOGN;szr++){
        for (int r = 0;r < n;r++){
            if (r + (1 << szr) > n) break;
            for (int c = 0;c < n;c++){
                rmq[szr][r][0][c] = max(rmq[szr - 1][r][0][c] , rmq[szr - 1][r + (1 << (szr - 1))][0][c]);
            }
        }
    }
    for (int szc = 1;szc <= LOGN;szc++){
        for (int c = 0;c < n;c++){
            if (c + (1 << szc) > n) break;
            for (int r = 0;r < n;r++){
                rmq[0][r][szc][c] = max(rmq[0][r][szc - 1][c] , rmq[0][r][szc - 1][c + (1 << (szc - 1))]);
            }
        }
    }
    for (int szr = 1;szr <= LOGN;szr++){
        for (int r = 0;r < n;r++){
            if (r + (1 << szr) > n) break;
            for (int szc = 1;szc <= LOGN;szc++){
                for (int c = 0;c < n;c++){
                    if (c + (1 << szc) > n) break;
                    rmq[szr][r][szc][c] = max(rmq[szr - 1][r][szc - 1][c] , rmq[szr - 1][r + (1 << (szr - 1))][szc - 1][c]);
                    rmq[szr][r][szc][c] = max({rmq[szr][r][szc][c] , rmq[szr - 1][r][szc - 1][c + (1 << (szc - 1))] , rmq[szr - 1][r + (1 << (szr - 1))][szc - 1][c + (1 << (szc - 1))]});
                }
            }
        }
    }
}

int qry(int l,int r){
    int sz = r - l + 1,res = 0,row,col;
    row = l;
    for (int i = 0;i <= LOGN;i++){
        if (sz & (1 << i)){
            col = l;
            for (int j = 0;j <= LOGN;j++){
                if (sz & (1 << j)){
                    res = max(res , rmq[i][row][j][col]);
                    col += (1 << j);
                }
            }
            row += (1 << i);
        }
    }
    //assert(row == r + 1 && col == r + 1);
    return res;
}

int rec(int l,int r){
    if (l == r){
        return rmq[0][l][0][r] = val[l];
    }
    if (rmq[0][l][0][r] != -1)
        return rmq[0][l][0][r];
    return rmq[0][l][0][r] = rec(l , r - 1) ^ rec(l + 1 , r);
}

void solve(int tt){
    cin >> n;
    for (int i = 15;~i;i--){
        if (n & (1 << i)){
            LOGN = i;
            break;
        }
    }

    rmq.clear();
    rmq.resize(LOGN + 1,vvvi(n, vvi(LOGN + 1, vi(n , - 1))));

    for (int i = 0;i < n;i++){
        cin >> val[i];
    }

    int res = 0;
    rec(0 , n - 1);
    pre();

    int q,l,r;
    cin >> q;
    while(q--){
        cin >> l >> r;
        --l;--r;
        cout << qry(l,r) << "\n";
    }
}

int main(){

    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int tt = 0,t = 1;
    //cin >> t;
    while(t--){solve(++tt);}

    cerr << "processor time: " << clock() / (double) CLOCKS_PER_SEC << "s    ";

    return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define dracarys ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("avx2")

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define dbg(x) cout << "[ " << x << " ] "
#define ump unordered_map

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash{
    int operator()(int x) const { return x ^ RANDOM; }
};

typedef vector<vector<vector<vector<int>>>>  vvvvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;

const int mod = 1e9 + 7;
const int mxN = 5e3 + 5;
int val[mxN];
vvvvi rmq;

int inline max(const int x,const int y){return (x > y ? x : y);}
int inline max(const std::vector<int> v){
    int mx = v[0];
    for (const int& x : v){
        mx = (x > mx ? x : mx);
    }
    return mx;
}
int n;
int LOGN;
void pre(){
    for (int szr = 1;szr <= LOGN;szr++){
        for (int r = 0;r < n;r++){
            if (r + (1 << szr) > n) break;
            for (int c = 0;c < n;c++){
                rmq[szr][r][0][c] = max(rmq[szr - 1][r][0][c] , rmq[szr - 1][r + (1 << (szr - 1))][0][c]);
            }
        }
    }
    for (int szc = 1;szc <= LOGN;szc++){
        for (int c = 0;c < n;c++){
            if (c + (1 << szc) > n) break;
            for (int r = 0;r < n;r++){
                rmq[0][r][szc][c] = max(rmq[0][r][szc - 1][c] , rmq[0][r][szc - 1][c + (1 << (szc - 1))]);
            }
        }
    }
    for (int szr = 1;szr <= LOGN;szr++){
        for (int r = 0;r < n;r++){
            if (r + (1 << szr) > n) break;
            for (int szc = 1;szc <= LOGN;szc++){
                for (int c = 0;c < n;c++){
                    if (c + (1 << szc) > n) break;
                    rmq[szr][r][szc][c] = max(rmq[szr - 1][r][szc - 1][c] , rmq[szr - 1][r + (1 << (szr - 1))][szc - 1][c]);
                    rmq[szr][r][szc][c] = max({rmq[szr][r][szc][c] , rmq[szr - 1][r][szc - 1][c + (1 << (szc - 1))] , rmq[szr - 1][r + (1 << (szr - 1))][szc - 1][c + (1 << (szc - 1))]});
                }
            }
        }
    }
}

int qry(int l,int r){
    int sz = r - l + 1,res = 0,row,col;
    row = l;
    for (int i = 0;i <= LOGN;i++){
        if (sz & (1 << i)){
            col = l;
            for (int j = 0;j <= LOGN;j++){
                if (sz & (1 << j)){
                    res = max(res , rmq[i][row][j][col]);
                    col += (1 << j);
                }
            }
            row += (1 << i);
        }
    }
    //assert(row == r + 1 && col == r + 1);
    return res;
}

vvi dp(mxN , vi(mxN , -1)),dp2(mxN , vi(mxN , -1));
int rec(int l,int r){
    if (l == r){
        return dp[l][r] = val[l];
    }
    if (dp[l][r] != -1)
        return dp[l][r];
    return dp[l][r] = rec(l , r - 1) ^ rec(l + 1 , r);
}

int REC(int l,int r){
    if (l == r){
        return dp2[l][r] = val[l];
    }
    if (dp2[l][r] != -1)
        return dp2[l][r];
    return dp2[l][r] = max({dp[l][r] , REC(l + 1 , r) , REC(l , r - 1)});
}

void solve(int tt){
    cin >> n;

    for (int i = 0;i < n;i++){
        cin >> val[i];
    }

    int res = 0;
    rec(0 , n - 1);
    REC(0 , n - 1);

    int q,l,r;
    cin >> q;
    while(q--){
        cin >> l >> r;
        --l;--r;
        cout << dp2[l][r] << "\n";
    }
}

int main(){

    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int tt = 0,t = 1;
    //cin >> t;
    while(t--){solve(++tt);}

    cerr << "processor time: " << clock() / (double) CLOCKS_PER_SEC << "s    ";

    return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define dracarys ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("avx2")

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define dbg(x) cout << "[ " << x << " ] "
#define ump unordered_map

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash{
    int operator()(int x) const { return x ^ RANDOM; }
};

typedef vector<vector<vector<vector<int>>>>  vvvvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;

const int mod = 1e9 + 7;
const int mxN = 5e2 + 5;
int val[mxN];
vvvvi rmq;

int inline max(const int x,const int y){return (x > y ? x : y);}
int inline max(const std::vector<int> v){
    int mx = v[0];
    for (const int& x : v){
        mx = (x > mx ? x : mx);
    }
    return mx;
}
int n;
int LOGN;
void pre(){
    for (int szr = 1;szr <= LOGN;szr++){
        for (int r = 0;r < n;r++){
            if (r + (1 << szr) > n) break;
            for (int c = 0;c < n;c++){
                rmq[szr][r][0][c] = max(rmq[szr - 1][r][0][c] , rmq[szr - 1][r + (1 << (szr - 1))][0][c]);
            }
        }
    }
    for (int szc = 1;szc <= LOGN;szc++){
        for (int c = 0;c < n;c++){
            if (c + (1 << szc) > n) break;
            for (int r = 0;r < n;r++){
                rmq[0][r][szc][c] = max(rmq[0][r][szc - 1][c] , rmq[0][r][szc - 1][c + (1 << (szc - 1))]);
            }
        }
    }
    for (int szr = 1;szr <= LOGN;szr++){
        for (int r = 0;r < n;r++){
            if (r + (1 << szr) > n) break;
            for (int szc = 1;szc <= LOGN;szc++){
                for (int c = 0;c < n;c++){
                    if (c + (1 << szc) > n) break;
                    rmq[szr][r][szc][c] = max(rmq[szr - 1][r][szc - 1][c] , rmq[szr - 1][r + (1 << (szr - 1))][szc - 1][c]);
                    rmq[szr][r][szc][c] = max({rmq[szr][r][szc][c] , rmq[szr - 1][r][szc - 1][c + (1 << (szc - 1))] , rmq[szr - 1][r + (1 << (szr - 1))][szc - 1][c + (1 << (szc - 1))]});
                }
            }
        }
    }
}

int qry(int l,int r){
    int sz = r - l + 1,res = 0,row,col;
    row = l;
    for (int i = 0;i <= LOGN;i++){
        if (sz & (1 << i)){
            col = l;
            for (int j = 0;j <= LOGN;j++){
                if (sz & (1 << j)){
                    res = max(res , rmq[i][row][j][col]);
                    col += (1 << j);
                }
            }
            row += (1 << i);
        }
    }
    //assert(row == r + 1 && col == r + 1);
    return res;
}

vvi dp(mxN , vi(mxN , -1)),dp2(mxN , vi(mxN , -1));
int rec(int l,int r){
    if (l == r){
        return dp[l][r] = val[l];
    }
    if (dp[l][r] != -1)
        return dp[l][r];
    return dp[l][r] = rec(l , r - 1) ^ rec(l + 1 , r);
}

int REC(int l,int r){
    if (l == r){
        return dp2[l][r] = val[l];
    }
    if (dp2[l][r] != -1)
        return dp2[l][r];
    return dp2[l][r] = max({dp[l][r] , REC(l + 1 , r) , REC(l , r - 1)});
}

void solve(int tt){
    cin >> n;

    for (int i = 0;i < n;i++){
        cin >> val[i];
    }

    int res = 0;
    rec(0 , n - 1);
    REC(0 , n - 1);

    int q,l,r;
    cin >> q;
    while(q--){
        cin >> l >> r;
        --l;--r;
        cout << dp2[l][r] << "\n";
    }
}

int main(){

    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int tt = 0,t = 1;
    //cin >> t;
    while(t--){solve(++tt);}

    cerr << "processor time: " << clock() / (double) CLOCKS_PER_SEC << "s    ";

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef pair<ll,ll> pll;
typedef vector<vector<ii>> vvii;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define EPS 1e-9
#define INF 100000
#define MOD 1000000007
#define PI 3.14159265358979323846

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n;
	cin>>n;
	int arr[n+1],dp[n+1][n+1],pref[n+1];
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	memset(dp,0,sizeof(dp));
	pref[0]=0;
	for(int i=1;i<=n;i++)
		pref[i]=pref[i-1]^arr[i];

	for(int si=1;si<=n;si++)
	{
		for(int i=1;i<=n-si+1;i++)
			dp[i][i+si] = max( dp[i][i+si-1] , max(dp[i+1][i+si],pref[i+si]^pref[i-1]));
	}
	int t,l,r;
	cin>>t;
	while(t--)
	{
		cin>>l>>r;
		cout<<dp[l][r]<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef pair<ll,ll> pll;
typedef vector<vector<ii>> vvii;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define EPS 1e-9
#define INF 100000
#define MOD 1000000007
#define PI 3.14159265358979323846

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n;
	cin>>n;
	int arr[n+1],dp[n+1][n+1],pref[n+1];
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	memset(dp,0,sizeof(dp));
	pref[0]=0;
	for(int i=1;i<=n;i++)
		pref[i]=pref[i-1]^arr[i];

	for(int si=1;si<=n;si++)
	{
		for(int i=1;i<=n-si+1;i++)
			dp[i][i+si-1] = max( dp[i][i+si-2] , max(dp[i+1][i+si-1],pref[i+si-1]^pref[i-1]));
	}
	int t,l,r;
	cin>>t;
	while(t--)
	{
		cin>>l>>r;
		cout<<dp[l][r]<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef pair<ll,ll> pll;
typedef vector<vector<ii>> vvii;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define EPS 1e-9
#define INF 100000
#define MOD 1000000007
#define PI 3.14159265358979323846

int calc(int *arr,int l,int r)
{
	if(r==l)
		return arr[l];
	if(r==l+1)
		return arr[l]^arr[r];
	int si = l-r+1;
	if(si%2)
		return arr[l]^arr[r];
	else
		return arr[l]^arr[l+1]^arr[r-1]^arr[r];

}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n;
	cin>>n;
	int arr[n+1],dp[n+2][n+2],f[n+2][n+2];
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	memset(dp,0,sizeof(dp));

	for(int si=1;si<=n;si++)
	{
		for(int i=1;i<=n-si+1;i++)
		{
			if(si==1)
				f[i][i] = arr[i];
			else
				f[i][i+si-1] = f[i+1][i+si-1]^f[i][i+si-2];

			dp[i][i+si-1] = max( dp[i][i+si-2] , max(dp[i+1][i+si-1],f[i][i+si-1] ));
		}

	}
	/*for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++)
			cout<<dp[i][j]<<" ";
		cout<<endl;
	}*/
	int t,l,r;
	cin>>t;
	while(t--)
	{
		cin>>l>>r;
		cout<<dp[l][r]<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef pair<ll,ll> pll;
typedef vector<vector<ii>> vvii;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define EPS 1e-9
#define INF 100000
#define MOD 1000000007
#define PI 3.14159265358979323846

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n;
	cin>>n;
	int arr[n+1],dp[n+2][n+2],pref[n+1];
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	memset(dp,0,sizeof(dp));
	pref[0]=0;
	for(int i=1;i<=n;i++)
		pref[i]=pref[i-1]^arr[i];

	for(int si=1;si<=n;si++)
	{
		for(int i=1;i<=n-si+1;i++)
			dp[i][i+si-1] = max( dp[i][i+si-2] , max(dp[i+1][i+si-1],pref[i+si-1]^pref[i-1]));
	}

	int t,l,r;
	cin>>t;
	while(t--)
	{
		cin>>l>>r;
		cout<<dp[l][r]<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef pair<ll,ll> pll;
typedef vector<vector<ii>> vvii;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define EPS 1e-9
#define INF 100000
#define MOD 1000000007
#define PI 3.14159265358979323846

int calc(int *arr,int l,int r)
{
	if(r==l)
		return arr[l];
	if(r==l+1)
		return arr[l]^arr[r];
	int si = l-r+1;
	if(si%2)
		return arr[l]^arr[r];
	else
		return arr[l]^arr[l+1]^arr[r-1]^arr[r];

}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n;
	cin>>n;
	int arr[n+1],dp[n+2][n+2],pref[n+1];
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	memset(dp,0,sizeof(dp));
	pref[0]=0;

	for(int si=1;si<=n;si++)
	{
		for(int i=1;i<=n-si+1;i++)
			dp[i][i+si-1] = max( dp[i][i+si-2] , max(dp[i+1][i+si-1],calc(arr,i,i+si-1)));
	}
	/*for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++)
			cout<<dp[i][j]<<" ";
		cout<<endl;
	}*/
	int t,l,r;
	cin>>t;
	while(t--)
	{
		cin>>l>>r;
		cout<<dp[l][r]<<endl;
	}
	return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>

using namespace std;

int dx[] = { 0, 0, -1,    1, -1, -1,  1, 1};
int dy[] = {-1, 1,  0,    0, -1,  1, -1, 1};

typedef long long int           ll;
typedef unsigned long long int  llu;
typedef pair<int,int>           pr;

#define     pf(a)                   cout<<a<<endl
#define     sci(a)                  scanf("%d",&a)
#define     scii(a, b)              scanf("%d %d",&a, &b)
#define     sciii(a, b, c)           scanf("%d %d %d",&a, &b, &c)
#define     scl(a)                  scanf("%lld", &a)
#define     scll(a, b)              scanf("%lld %lld", &a, &b)
#define     sclll(a, b, c)          scanf("%lld %lld %lld", &a, &b, &c)
#define     MAX3(a,b,c)              max(a,max(b,c))
#define     MAX4(a,b,c,d)            max(max(a,b),max(c,d))
#define     umap                    unordered_map
#define     mem(a,b)                memset(a,b,sizeof(a))
#define     IO                      ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define     READ(f)                 freopen(f,"r",stdin)
#define     WRITE(f)                freopen(f,"w",stdout)

#define     mod                     1000000007
#define     base                    10000007
#define     eps                     1e-9
#define     INF                     1llu<<61
#define     inf                     1<<29
#define     PI                      acos(-1.0) //3.14159.....

#define     mx                      5007

////

int n,l,r,q,ar[mx],clc[mx][mx],dp[mx][mx],ans[mx][mx];

int call(int i,int j)
{
    if(i==j) return 1;
    if(i>j ) return 0;
    if(dp[i][j] != -1) return dp[i][j];

    int ans = 0;

    ans = MAX3(ans, clc[j-i][i], call(i,j-1));
    ans = MAX3(ans, clc[j-i][i], call(i+1,j-1));
    ans = MAX3(ans, clc[j-i][i], call(i+1,j));

    return dp[i][j] = max(dp[i][j], ans);
}
int brute(int x,int y)
{
    int ans = 1;
    for(int i=x;i<=y;i++){
        for(int j=i+1;j<=y;j++){
            ans = max(ans, clc[j-i][i]);
        }
    }
    return ans;
}
int main()
{
//    READ("in.txt");
//    WRITE("out.txt");
//    srand(time(0));
//    cout<<100<<endl;
//    for(int i=0;i<100;i++) cout<<rand()%500<<" ";

    sci(n);
    for(int i=1;i<=n;i++) sci(ar[i]), clc[0][i] = ar[i];

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-i;j++){
            clc[i][j] = clc[i-1][j] ^ clc[i-1][j+1];
            //ans[i][j] = max(clc[i][j],clc)
        }
    }
//    for(int i=1;i<n;i++){
//        ans[i][i] = 1;
//        int tmp = 0,x=n-i+1;
//        for(int j=n-i;j>0;j--){
//            tmp = max(tmp, clc[i][j]);
//            ans[i][x] = tmp;
//        }
//    }
//    cout<<endl;
//    for(int i=1;i<=n;i++) printf("%3d ",i);
//    cout<<endl<<endl;
//    for(int i=0;i<=n;i++){
//        for(int j=1;j<=n-i;j++) printf("%3d ",clc[i][j]);
//        cout<<endl;
//    }
//    cout<<endl<<endl;

//    int q;
//    sci(q);
//    while(q--){
//        scii(l,r);
//        cout<<ans[l][r]<<endl;
//    }

    mem(dp,-1);
    int tmp = call(1,n);

//    for(int i=1;i<=100;i++){
//        for(int j=i+1;j<=100;j++){
//            if(brute(i,j) != call(i,j)){
//                cout<<i<<" "<<j<<endl;
//                cout<<brute(i,j)<<" "<<call(i,j)<<endl<<endl;
//            }
//        }
//
//    }

    int q,one=1;
    sci(q);
    while(q--){
        scii(l,r);

        printf("%d\n",(l==r) ? 1 : call(l,r));
    }

    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>

using namespace std;

int dx[] = { 0, 0, -1,    1, -1, -1,  1, 1};
int dy[] = {-1, 1,  0,    0, -1,  1, -1, 1};

typedef long long int           ll;
typedef unsigned long long int  llu;
typedef pair<int,int>           pr;

#define     pf(a)                   cout<<a<<endl
#define     sci(a)                  scanf("%d",&a)
#define     scii(a, b)              scanf("%d %d",&a, &b)
#define     sciii(a, b, c)           scanf("%d %d %d",&a, &b, &c)
#define     scl(a)                  scanf("%lld", &a)
#define     scll(a, b)              scanf("%lld %lld", &a, &b)
#define     sclll(a, b, c)          scanf("%lld %lld %lld", &a, &b, &c)
#define     MAX3(a,b,c)              max(a,max(b,c))
#define     MAX4(a,b,c,d)            max(max(a,b),max(c,d))
#define     umap                    unordered_map
#define     mem(a,b)                memset(a,b,sizeof(a))
#define     IO                      ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define     READ(f)                 freopen(f,"r",stdin)
#define     WRITE(f)                freopen(f,"w",stdout)

#define     mx                      5007
#define     mod                     1000000007
#define     base                    10000007
#define     eps                     1e-9
#define     INF                     1llu<<61
#define     inf                     1<<29
#define     PI                      acos(-1.0) //3.14159.....

////

int n,l,r,q,ar[mx],clc[mx][mx],dp[mx][mx];

int call(int i,int j)
{
    if(i>=j || i>r || j<l) return 0;
    if(dp[i][j] != -1) return dp[i][j];

    int ans = 0;

    ans = MAX3(ans, clc[j-i][i], call(i,j-1));
    ans = MAX3(ans, clc[j-i][i], call(i+1,j));

    return dp[i][j] = ans;
}
int main()
{
    //cout<<(8^4);
    sci(n);
    for(int i=1;i<=n;i++) sci(ar[i]), clc[0][i] = ar[i];

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-i;j++){
            clc[i][j] = clc[i-1][j] ^ clc[i-1][j+1];
            //ans[i][j] = max(clc[i][j],clc)
        }
    }

    mem(dp,-1);
    int q;
    sci(q);
    while(q--){
        scii(l,r);
        printf("%d\n",call(l,r));
    }

    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>

using namespace std;

int dx[] = { 0, 0, -1,    1, -1, -1,  1, 1};
int dy[] = {-1, 1,  0,    0, -1,  1, -1, 1};

typedef long long int           ll;
typedef unsigned long long int  llu;
typedef pair<int,int>           pr;

#define     pf(a)                   cout<<a<<endl
#define     sci(a)                  scanf("%d",&a)
#define     scii(a, b)              scanf("%d %d",&a, &b)
#define     sciii(a, b, c)           scanf("%d %d %d",&a, &b, &c)
#define     scl(a)                  scanf("%lld", &a)
#define     scll(a, b)              scanf("%lld %lld", &a, &b)
#define     sclll(a, b, c)          scanf("%lld %lld %lld", &a, &b, &c)
#define     MAX3(a,b,c)              max(a,max(b,c))
#define     MAX4(a,b,c,d)            max(max(a,b),max(c,d))
#define     umap                    unordered_map
#define     mem(a,b)                memset(a,b,sizeof(a))
#define     IO                      ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define     READ(f)                 freopen(f,"r",stdin)
#define     WRITE(f)                freopen(f,"w",stdout)

#define     mod                     1000000007
#define     base                    10000007
#define     eps                     1e-9
#define     INF                     1llu<<61
#define     inf                     1<<29
#define     PI                      acos(-1.0) //3.14159.....

#define     mx                      5007

////

int n,l,r,q,ar[mx],clc[mx][mx],dp[mx][mx],ans[mx][mx];

int call(int i,int j)
{
    if(i==j) return 1;
    if(i>j ) return 0;
    if(dp[i][j] != -1) return dp[i][j];

    int ans = 0,tmp=0;

    tmp = MAX3(tmp, clc[j-i][i], call(i,j-1));
    tmp = MAX3(tmp, clc[j-i][i], call(i+1,j-1));
    ans = MAX3(tmp, clc[j-i][i], call(i+1,j));

    ans = max(ans,tmp);
    return dp[i][j] = ans;
}
int brute(int x,int y)
{
    int ans = 1;
    for(int i=x;i<=y;i++){
        for(int j=i+1;j<=y;j++){
            ans = max(ans, clc[j-i][i]);
        }
    }
    return ans;
}
int main()
{

    sci(n);
    for(int i=1;i<=n;i++) sci(ar[i]), clc[0][i] = ar[i];

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-i;j++){
            clc[i][j] = clc[i-1][j] ^ clc[i-1][j+1];

        }
    }

    for(int i=1;i<n;i++){
        for(int j=n-i;j>0;j--){
            clc[i][j] = MAX3(clc[i][j], clc[i-1][j], clc[i-1][j+1]);
        }
    }

    int q;
    sci(q);
    while(q--){
        scii(l,r);
        if(l==r) printf("%d\n",ar[l]);
        else printf("%d\n",clc[r-l][l]);
    }

    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>

using namespace std;

int dx[] = { 0, 0, -1,    1, -1, -1,  1, 1};
int dy[] = {-1, 1,  0,    0, -1,  1, -1, 1};

typedef long long int           ll;
typedef unsigned long long int  llu;
typedef pair<int,int>           pr;

#define     pf(a)                   cout<<a<<endl
#define     sci(a)                  scanf("%d",&a)
#define     scii(a, b)              scanf("%d %d",&a, &b)
#define     sciii(a, b, c)           scanf("%d %d %d",&a, &b, &c)
#define     scl(a)                  scanf("%lld", &a)
#define     scll(a, b)              scanf("%lld %lld", &a, &b)
#define     sclll(a, b, c)          scanf("%lld %lld %lld", &a, &b, &c)
#define     MAX3(a,b,c)              max(a,max(b,c))
#define     MAX4(a,b,c,d)            max(max(a,b),max(c,d))
#define     umap                    unordered_map
#define     mem(a,b)                memset(a,b,sizeof(a))
#define     IO                      ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define     READ(f)                 freopen(f,"r",stdin)
#define     WRITE(f)                freopen(f,"w",stdout)

#define     mod                     1000000007
#define     base                    10000007
#define     eps                     1e-9
#define     INF                     1llu<<61
#define     inf                     1<<29
#define     PI                      acos(-1.0) //3.14159.....

#define     mx                      5007

////

int n,l,r,q,ar[mx],clc[mx][mx],dp[mx][mx],ans[mx][mx];

int call(int i,int j)
{
    if(i==j) return 1;
    if(i>j ) return 0;
    if(dp[i][j] != -1) return dp[i][j];

    int ans = 0;

    ans = MAX3(ans, clc[j-i][i], call(i,j-1));
    ans = MAX3(ans, clc[j-i][i], call(i+1,j-1));
    ans = MAX3(ans, clc[j-i][i], call(i+1,j));

    return dp[i][j] = ans;
}
int brute(int x,int y)
{
    int ans = 1;
    for(int i=x;i<=y;i++){
        for(int j=i+1;j<=y;j++){
            ans = max(ans, clc[j-i][i]);
        }
    }
    return ans;
}
int main()
{
//    READ("in.txt");
//    WRITE("out.txt");
//    srand(time(0));
//    cout<<100<<endl;
//    for(int i=0;i<100;i++) cout<<rand()%500<<" ";

    sci(n);
    for(int i=1;i<=n;i++) sci(ar[i]), clc[0][i] = ar[i];

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-i;j++){
            clc[i][j] = clc[i-1][j] ^ clc[i-1][j+1];
            //ans[i][j] = max(clc[i][j],clc)
        }
    }
//    cout<<endl;
//    for(int i=1;i<=n;i++) printf("%3d ",i);
//    cout<<endl<<endl;
//    for(int i=0;i<=n;i++){
//        for(int j=1;j<=n-i;j++) printf("%3d ",clc[i][j]);
//        cout<<endl;
//    }
//    cout<<endl<<endl;

    //for(int i=1;i<=n;i++) printf("%3d ",ar[i]);
    for(int i=0;i<=n;i++) clc[0][i] = 0;

    for(int i=1;i<n;i++) ans[i][i+1] = clc[1][i];
    for(int i=2;i<n;i++){
        ans[i][i] = 1;
        int tmp = 0,x=n-i+1;
        for(int j=n-i;j>0;j--){
            //tmp = max(tmp, clc[i][j]);
            //ans[i][n] = max(ans[i][n],tmp);
            ans[j][j+i] = MAX4(ans[j][j+i], clc[i][j], ans[j][j+i-1], ans[j+1][j+i]);
        }
    }

//    cout<<endl;
//    for(int i=1;i<=n;i++) printf("%3d ",i);
//    cout<<endl<<endl;
//    for(int i=1;i<=n;i++){
//        for(int j=1;j<=n;j++) printf("%3d ",ans[i][j]);
//        cout<<endl;
//    }
//    cout<<endl<<endl;

    int q;
    sci(q);
    while(q--){
        scii(l,r);
        if(l==r) printf("1\n");
        else printf("%d\n",ans[l][r]);
    }

//    mem(dp,-1);
//    int tmp = call(1,n);
//
//
//    for(int i=1;i<=100;i++){
//        for(int j=i+1;j<=100;j++){
//            if(brute(i,j) != call(i,j)){
//                cout<<i<<" "<<j<<endl;
//                cout<<brute(i,j)<<" "<<call(i,j)<<endl<<endl;
//            }
//        }
//
//    }
//
////    int q,one=1;
////    sci(q);
////    while(q--){
////        scii(l,r);
////
////        printf("%d\n",(l==r) ? 1 : call(l,r));
////    }

    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>

using namespace std;

int dx[] = { 0, 0, -1,    1, -1, -1,  1, 1};
int dy[] = {-1, 1,  0,    0, -1,  1, -1, 1};

typedef long long int           ll;
typedef unsigned long long int  llu;
typedef pair<int,int>           pr;

#define     pf(a)                   cout<<a<<endl
#define     sci(a)                  scanf("%d",&a)
#define     scii(a, b)              scanf("%d %d",&a, &b)
#define     sciii(a, b, c)           scanf("%d %d %d",&a, &b, &c)
#define     scl(a)                  scanf("%lld", &a)
#define     scll(a, b)              scanf("%lld %lld", &a, &b)
#define     sclll(a, b, c)          scanf("%lld %lld %lld", &a, &b, &c)
#define     MAX3(a,b,c)              max(a,max(b,c))
#define     MAX4(a,b,c,d)            max(max(a,b),max(c,d))
#define     umap                    unordered_map
#define     mem(a,b)                memset(a,b,sizeof(a))
#define     IO                      ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define     READ(f)                 freopen(f,"r",stdin)
#define     WRITE(f)                freopen(f,"w",stdout)

#define     mx                      5007
#define     mod                     1000000007
#define     base                    10000007
#define     eps                     1e-9
#define     INF                     1llu<<61
#define     inf                     1<<29
#define     PI                      acos(-1.0) //3.14159.....

////

int n,l,r,q,ar[mx],clc[mx][mx],dp[mx][mx];

int call(int i,int j)
{
    if(i>=j ) return 0;
    if(dp[i][j] != -1) return dp[i][j];

    int ans = 0;

    ans = MAX3(ans, clc[j-i][i], call(i,j-1));
    ans = MAX3(ans, clc[j-i][i], call(i+1,j));

    return dp[i][j] = ans;
}
int main()
{
    //cout<<(8^4);
    sci(n);
    for(int i=1;i<=n;i++) sci(ar[i]), clc[0][i] = ar[i];

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-i;j++){
            clc[i][j] = clc[i-1][j] ^ clc[i-1][j+1];
            //ans[i][j] = max(clc[i][j],clc)
        }
    }

    int q;
    sci(q);
    while(q--){
        scii(l,r);
        mem(dp,-1);
        printf("%d\n",call(l,r));
    }

    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>

using namespace std;

int dx[] = { 0, 0, -1,    1, -1, -1,  1, 1};
int dy[] = {-1, 1,  0,    0, -1,  1, -1, 1};

typedef long long int           ll;
typedef unsigned long long int  llu;
typedef pair<int,int>           pr;

#define     pf(a)                   cout<<a<<endl
#define     sci(a)                  scanf("%d",&a)
#define     scii(a, b)              scanf("%d %d",&a, &b)
#define     sciii(a, b, c)           scanf("%d %d %d",&a, &b, &c)
#define     scl(a)                  scanf("%lld", &a)
#define     scll(a, b)              scanf("%lld %lld", &a, &b)
#define     sclll(a, b, c)          scanf("%lld %lld %lld", &a, &b, &c)
#define     MAX3(a,b,c)              max(a,max(b,c))
#define     MAX4(a,b,c,d)            max(max(a,b),max(c,d))
#define     umap                    unordered_map
#define     mem(a,b)                memset(a,b,sizeof(a))
#define     IO                      ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define     READ(f)                 freopen(f,"r",stdin)
#define     WRITE(f)                freopen(f,"w",stdout)

#define     mod                     1000000007
#define     base                    10000007
#define     eps                     1e-9
#define     INF                     1llu<<61
#define     inf                     1<<29
#define     PI                      acos(-1.0) //3.14159.....

#define     mx                      5007

////

int n,l,r,q,ar[mx],clc[mx][mx],dp[mx][mx],ans[mx][mx];

int call(int i,int j)
{
    if(i==j) return 1;
    if(i>j ) return 0;
    if(dp[i][j] != -1) return dp[i][j];

    int ans = 0,tmp=0;

    tmp = MAX3(tmp, clc[j-i][i], call(i,j-1));
    tmp = MAX3(tmp, clc[j-i][i], call(i+1,j-1));
    ans = MAX3(tmp, clc[j-i][i], call(i+1,j));

    ans = max(ans,tmp);
    return dp[i][j] = ans;
}
int brute(int x,int y)
{
    int ans = 1;
    for(int i=x;i<=y;i++){
        for(int j=i+1;j<=y;j++){
            ans = max(ans, clc[j-i][i]);
        }
    }
    return ans;
}
int main()
{
//    READ("in.txt");
//    WRITE("out.txt");
//    srand(time(0));
//    cout<<100<<endl;
//    for(int i=0;i<100;i++) cout<<rand()%500<<" ";

    sci(n);
    for(int i=1;i<=n;i++) sci(ar[i]), clc[0][i] = ar[i];

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-i;j++){
            clc[i][j] = clc[i-1][j] ^ clc[i-1][j+1];
            //ans[i][j] = max(clc[i][j],clc)
        }
    }
//    cout<<endl;
//    for(int i=1;i<=n;i++) printf("%3d ",i);
//    cout<<endl<<endl;
//    for(int i=0;i<=n;i++){
//        for(int j=1;j<=n-i;j++) printf("%3d ",clc[i][j]);
//        cout<<endl;
//    }
//    cout<<endl<<endl;

    //for(int i=1;i<=n;i++) printf("%3d ",ar[i]);
    for(int i=0;i<=n;i++) clc[0][i] = 0;

    for(int i=1;i<n;i++) ans[i][i+1] = clc[1][i];
    for(int i=2;i<n;i++){
        //ans[i][i] = 1;
        int tmp = 0,x=n-i+1;
        for(int j=n-i;j>0;j--){
            //tmp = max(tmp, clc[i][j]);
            //ans[1][n] = max(ans[i][n],tmp);
            //ans[j][j+i] = MAX4(ans[j][j+i], clc[i][j], ans[j][j+i-1], ans[j+1][j+i]);
            clc[i][j] = MAX3(clc[i][j], clc[i-1][j], clc[i-1][j+1]);
        }
    }

//    cout<<endl;
//    for(int i=1;i<=n;i++) printf("%3d ",i);
//    cout<<endl<<endl;
//    for(int i=1;i<=n;i++){
//        for(int j=1;j<=n;j++) printf("%3d ",clc[i][j]);
//        cout<<endl;
//    }
//    cout<<endl<<endl;

    int q;
    sci(q);
    while(q--){
        scii(l,r);
        if(l==r) printf("%d\n",ar[l]);
        else printf("%d\n",clc[r-l][l]);
    }

//    mem(dp,-1);
//    int tmp = call(1,n);
//
//
//    for(int i=1;i<=100;i++){
//        for(int j=i+1;j<=100;j++){
//            if(brute(i,j) != call(i,j)){
//                cout<<i<<" "<<j<<endl;
//                cout<<brute(i,j)<<" "<<call(i,j)<<endl<<endl;
//            }
//        }
//
//    }
//
////    int q,one=1;
////    sci(q);
////    while(q--){
////        scii(l,r);
////
////        printf("%d\n",(l==r) ? 1 : call(l,r));
////    }

    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>

using namespace std;

int dx[] = { 0, 0, -1,    1, -1, -1,  1, 1};
int dy[] = {-1, 1,  0,    0, -1,  1, -1, 1};

typedef long long int           ll;
typedef unsigned long long int  llu;
typedef pair<int,int>           pr;

#define     pf(a)                   cout<<a<<endl
#define     sci(a)                  scanf("%d",&a)
#define     scii(a, b)              scanf("%d %d",&a, &b)
#define     sciii(a, b, c)           scanf("%d %d %d",&a, &b, &c)
#define     scl(a)                  scanf("%lld", &a)
#define     scll(a, b)              scanf("%lld %lld", &a, &b)
#define     sclll(a, b, c)          scanf("%lld %lld %lld", &a, &b, &c)
#define     MAX3(a,b,c)              max(a,max(b,c))
#define     MAX4(a,b,c,d)            max(max(a,b),max(c,d))
#define     umap                    unordered_map
#define     mem(a,b)                memset(a,b,sizeof(a))
#define     IO                      ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define     READ(f)                 freopen(f,"r",stdin)
#define     WRITE(f)                freopen(f,"w",stdout)

#define     mx                      5007
#define     mod                     1000000007
#define     base                    10000007
#define     eps                     1e-9
#define     INF                     1llu<<61
#define     inf                     1<<29
#define     PI                      acos(-1.0) //3.14159.....

////

int n,l,r,q,ar[mx],clc[mx][mx],dp[mx][mx];

int call(int i,int j)
{
    if(i>=j ) return 0;
    if(dp[i][j] != -1) return dp[i][j];

    int ans = 0;

    ans = MAX3(ans, clc[j-i][i], call(i,j-1));
    ans = MAX3(ans, clc[j-i][i], call(i+1,j));

    return dp[i][j] = ans;
}
int main()
{
    //cout<<(8^4);
    sci(n);
    for(int i=1;i<=n;i++) sci(ar[i]), clc[0][i] = ar[i];

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-i;j++){
            clc[i][j] = clc[i-1][j] ^ clc[i-1][j+1];
            //ans[i][j] = max(clc[i][j],clc)
        }
    }

    mem(dp,-1);
    int tmp = call(1,n);

    int q,one=1;
    sci(q);
    while(q--){
        scii(l,r);

        printf("%d\n",(l==r) ? 1 : call(l,r));
    }

    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>

using namespace std;

int dx[] = { 0, 0, -1,    1, -1, -1,  1, 1};
int dy[] = {-1, 1,  0,    0, -1,  1, -1, 1};

typedef long long int           ll;
typedef unsigned long long int  llu;
typedef pair<int,int>           pr;

#define     pf(a)                   cout<<a<<endl
#define     sci(a)                  scanf("%d",&a)
#define     scii(a, b)              scanf("%d %d",&a, &b)
#define     sciii(a, b, c)           scanf("%d %d %d",&a, &b, &c)
#define     scl(a)                  scanf("%lld", &a)
#define     scll(a, b)              scanf("%lld %lld", &a, &b)
#define     sclll(a, b, c)          scanf("%lld %lld %lld", &a, &b, &c)
#define     MAX3(a,b,c)              max(a,max(b,c))
#define     MAX4(a,b,c,d)            max(max(a,b),max(c,d))
#define     umap                    unordered_map
#define     mem(a,b)                memset(a,b,sizeof(a))
#define     IO                      ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define     READ(f)                 freopen(f,"r",stdin)
#define     WRITE(f)                freopen(f,"w",stdout)

#define     mx                      5007
#define     mod                     1000000007
#define     base                    10000007
#define     eps                     1e-9
#define     INF                     1llu<<61
#define     inf                     1<<29
#define     PI                      acos(-1.0) //3.14159.....

////

int n,l,r,q,ar[mx],clc[mx][mx],dp[mx][mx];

int call(int i,int j)
{
    if(i==j) return ar[i];
    if(i>j) return 0;
    if(dp[i][j] != -1) return dp[i][j];

    int ans = 0;

    ans = MAX3(ans, clc[j-i][i], call(i,j-1));
    ans = MAX3(ans, clc[j-i][i], call(i+1,j));

    return dp[i][j] = ans;
}
int main()
{
    //cout<<(8^4);
    sci(n);
    for(int i=1;i<=n;i++) sci(ar[i]), clc[0][i] = ar[i];

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-i;j++){
            clc[i][j] = clc[i-1][j] ^ clc[i-1][j+1];
            //ans[i][j] = max(clc[i][j],clc)
        }
    }

    mem(dp,-1);
    int q;
    sci(q);
    while(q--){
        scii(l,r);
        printf("%d\n",call(l,r));
    }

    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>

using namespace std;

int dx[] = { 0, 0, -1,    1, -1, -1,  1, 1};
int dy[] = {-1, 1,  0,    0, -1,  1, -1, 1};

typedef long long int           ll;
typedef unsigned long long int  llu;
typedef pair<int,int>           pr;

#define     pf(a)                   cout<<a<<endl
#define     sci(a)                  scanf("%d",&a)
#define     scii(a, b)              scanf("%d %d",&a, &b)
#define     sciii(a, b, c)           scanf("%d %d %d",&a, &b, &c)
#define     scl(a)                  scanf("%lld", &a)
#define     scll(a, b)              scanf("%lld %lld", &a, &b)
#define     sclll(a, b, c)          scanf("%lld %lld %lld", &a, &b, &c)
#define     MAX(a,b,c)              max(a,max(b,c))
#define     MAX(a,b,c,d)            max(max(a,b),max(c,d))
#define     umap                    unordered_map
#define     mem(a,b)                memset(a,b,sizeof(a))
#define     IO                      ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define     READ(f)                 freopen(f,"r",stdin)
#define     WRITE(f)                freopen(f,"w",stdout)

#define     mx                      5007
#define     mod                     1000000007
#define     base                    10000007
#define     eps                     1e-9
#define     INF                     1llu<<61
#define     inf                     1<<29
#define     PI                      acos(-1.0) //3.14159.....

////

int n,l,r,q,ar[mx],clc[mx][mx],dp[mx][mx];

int call(int i,int j)
{
    if(i>=j || i>r || j<l) return 0;
    if(dp[i][j] != -1) return dp[i][j];

    int ans = 0;

    ans = MAX(ans, clc[j-i][i], call(i,j-1));
    ans = MAX(ans, clc[j-i][i], call(i+1,j));

    return dp[i][j] = ans;
}
int main()
{
    //cout<<(8^4);
    sci(n);
    for(int i=1;i<=n;i++) sci(ar[i]), clc[0][i] = ar[i];

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-i;j++){
            clc[i][j] = clc[i-1][j] ^ clc[i-1][j+1];
            //ans[i][j] = max(clc[i][j],clc)
        }
    }

    mem(dp,-1);
    int q;
    sci(q);
    while(q--){
        scii(l,r);
        printf("%d\n",call(l,r));
    }

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef LOCAL
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define trace(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#else
#define debug(...) 42
#define trace(a, n) 42
#endif
#define int long long
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define uint unsigned int
#define pi pair<int, int>
#define pl pair<ll, ll>
const int infi = 2e9;
const long long infl = 4e18;
const int md = 998244353;
const double PI = 3.14159265358979323846;
template <typename Map>bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
inline long long modpow(long long x, long long n) { long long res = 1; while (n > 0) { if (n % 2) res = (res * x) % md; x = (x * x) % md; n /= 2; } return res; }
inline long long inv(long long x) {return modpow(x, md-2);}
inline long long log_2 (long long n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline long long log_10 (long long n) { return (n > 9ll)? 1ll + log_10(n/10): 0ll;}
inline void add(long long& a, long long b) {a = ((a % md) + (b % md)) % md;}
inline void sub(long long& a, long long b) {a = ((a % md) - (b % md) + md) % md;}
inline void mul(long long& a, long long b) {a = ((a % md) * (b % md)) % md;}
inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r;} return a; }
inline long long randint (long long l, long long r) { uniform_int_distribution<long long> dis(l, r); return dis(rng); }
inline double random (double l, double r) { uniform_real_distribution<double> dis(l, r); return dis(rng); }

template <class T>
struct cmp {
  bool operator() (const T& x, const T& y) const {
    return x > y;
  }
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

#undef int

const int maxn = 5005;
int dp[maxn][maxn], ans[maxn][maxn];

signed main() {
#ifdef READ_FILE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> dp[i][i];
  for (int len = 2; len <= n; len++) {
    for (int i = 1; len + i - 1 <= n; i++) {
      int x = i;
      int y = len + i - 1;
      dp[x][y] = (dp[x][y - 1] ^ dp[x + 1][y]);
    }
  }
  for (int i = 1; i <= n; i++) ans[i][i] = dp[i][i];
  for (int len = 2; len <= n; len++) {
    for (int i = 1; len + i - 1 <= n; i++) {
      int x = i;
      int y = len + i - 1;
      int& c = ans[x][y];
      c = dp[x][y];
      c = max(c, ans[x][y - 1]);
      c = max(c, ans[x + 1][y]);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << ans[l][r] << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
#ifdef LOCAL
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define trace(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#else
#define debug(...) 42
#define trace(a, n) 42
#endif
#define int long long
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define uint unsigned int
#define pi pair<int, int>
#define pl pair<ll, ll>
const int infi = 2e9;
const long long infl = 4e18;
const int md = 998244353;
const double PI = 3.14159265358979323846;
template <typename Map>bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
inline long long modpow(long long x, long long n) { long long res = 1; while (n > 0) { if (n % 2) res = (res * x) % md; x = (x * x) % md; n /= 2; } return res; }
inline long long inv(long long x) {return modpow(x, md-2);}
inline long long log_2 (long long n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline long long log_10 (long long n) { return (n > 9ll)? 1ll + log_10(n/10): 0ll;}
inline void add(long long& a, long long b) {a = ((a % md) + (b % md)) % md;}
inline void sub(long long& a, long long b) {a = ((a % md) - (b % md) + md) % md;}
inline void mul(long long& a, long long b) {a = ((a % md) * (b % md)) % md;}
inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r;} return a; }
inline long long randint (long long l, long long r) { uniform_int_distribution<long long> dis(l, r); return dis(rng); }
inline double random (double l, double r) { uniform_real_distribution<double> dis(l, r); return dis(rng); }

template <class T>
struct cmp {
  bool operator() (const T& x, const T& y) const {
    return x > y;
  }
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

#undef int

const int maxn = 5005;
int dp[maxn][maxn], ans[maxn][maxn];

signed main() {
#ifdef READ_FILE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> dp[i][i];
  for (int len = 2; len <= n; len++) {
    for (int i = 1; len + i - 1 <= n; i++) {
      int x = i;
      int y = len + i - 1;
      dp[x][y] = (dp[x][y - 1] ^ dp[x + 1][y]);
    }
  }
  for (int len = 2; len <= n; len++) {
    for (int i = 1; len + i - 1 <= n; i++) {
      int x = i;
      int y = len + i - 1;
      int& c = ans[x][y];
      c = dp[x][y];
      c = max(c, ans[x][y - 1]);
      c = max(c, ans[x + 1][y]);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << ans[l][r] << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef LOCAL
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define trace(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#else
#define debug(...) 42
#define trace(a, n) 42
#endif
#define int long long
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define uint unsigned int
#define pi pair<int, int>
#define pl pair<ll, ll>
const int infi = 2e9;
const long long infl = 4e18;
const int md = 998244353;
const double PI = 3.14159265358979323846;
template <typename Map>bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
inline long long modpow(long long x, long long n) { long long res = 1; while (n > 0) { if (n % 2) res = (res * x) % md; x = (x * x) % md; n /= 2; } return res; }
inline long long inv(long long x) {return modpow(x, md-2);}
inline long long log_2 (long long n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline long long log_10 (long long n) { return (n > 9ll)? 1ll + log_10(n/10): 0ll;}
inline void add(long long& a, long long b) {a = ((a % md) + (b % md)) % md;}
inline void sub(long long& a, long long b) {a = ((a % md) - (b % md) + md) % md;}
inline void mul(long long& a, long long b) {a = ((a % md) * (b % md)) % md;}
inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r;} return a; }
inline long long randint (long long l, long long r) { uniform_int_distribution<long long> dis(l, r); return dis(rng); }
inline double random (double l, double r) { uniform_real_distribution<double> dis(l, r); return dis(rng); }

template <class T>
struct cmp {
  bool operator() (const T& x, const T& y) const {
    return x > y;
  }
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

#undef int

const int maxn = 5005;
int dp[maxn][maxn], ans[maxn][maxn];

signed main() {
#ifdef READ_FILE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> dp[i][i];
  for (int len = 2; len <= n; len++) {
    for (int i = 1; len + i - 1 <= n; i++) {
      int x = i;
      int y = len + i - 1;
      dp[x][y] = (dp[x][y - 1] ^ dp[x + 1][y]);
    }
  }
  for (int len = 2; len <= n; len++) {
    for (int i = 1; len + i - 1 <= n; i++) {
      int x = i;
      int y = len + i - 1;
      int& c = ans[x][y];
      c = dp[x][y];
      c = max(c, ans[x][y - 1]);
      c = max(c, ans[x + 1][y]);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << ans[l][r] << "\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 10000000007
bool compare1(pair<ll,ll> a1,pair<ll,ll> a2){
    double aa=((double)a1.first)/a1.second;
    double bb=((double)a2.first)/a2.second;
    return aa>bb;
}
// struct compare2{
//  bool operator()(pair a1,pair a2){
//      return a1.second>a2.second;
//  }
// };
// void rrrr(vector<vector<vector<ll>>> &arr){
//  cout<<arr[39][19][5]<<"--";
// }
// ll arr[500001];
// void update(ll arr[],ll qa,ll qe,ll sa,ll se,ll si,ll val){
//  if(qa<=sa && se<=qe){
//      arr[si]=val;
//      return;
//  }
//  if(qa>se || sa>qe)
//      return;
//  ll mid=(se+sa)/2;
//  update(arr,qa,qe,sa,mid,2*si+1,val);
//  update(arr,qa,qe,mid+1,se,2*si+2,val);
//  arr[si]=max(arr[2*si+1],arr[2*si+2]);

// }
// ll get_sum(ll arr[],ll qa,ll qe,ll sa,ll se,ll si){
//  if(qa<=sa && se<=qe){
//      return arr[si];
//  }
//  if(qa>se || sa>qe)
//      return 0;
//  ll mid=(se+sa)/2;
//  ll k1=get_sum(arr,qa,qe,sa,mid,2*si+1);
//  ll k2=get_sum(arr,qa,qe,mid+1,se,2*si+2);
//  return max(k1,k2);

// }

// long long max(long long a,long long b){
//  if(a<b)
//      return b;
//  return a;
// }
// ll convert(string a){
//  ll k=0;
//  ll t=1;
//  for(ll i=0;i<a.length();i++){
//      k=(k+(a[i]-'a'+1)*t)%10000000000009ll;
//      t=(t*31)%10000000000009ll;
//  }
//  return k;
// }
// ll convert(string a){
//  ll k=0;
//  ll t=1;
//  for(ll i=0;i<a.length();i++){
//      k=(k+(a[i]-'a'+1)*t)%10000000009ll;
//      t=(t*31)%10000000009ll;
//  }
//  return k;
// }
double min(double a,double b){
    if(a>b)
        return b;
    return a;
}

ll max(ll a,ll b){
    if(a>b)
        return a;
    return b;
}
ll hashs(string a){
 ll k=27;
 ll value=0;
 for(ll i=0;i<a.length();i++){
     value=((value*k+(a[i]-'a'+1)))%10000000000007;

 }
 return value;
}
ll find(ll parent[],ll i){
    if(parent[i]!=i){
        parent[i]=find(parent,parent[i]);
    }
    return parent[i];
}
void unions(ll parent[],ll i,ll j){
    ll k=find(parent,i);
    ll l=find(parent,j);
    parent[k]=l;
}

ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

// Function to return LCM of two numbers
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

// void dfs(vector<vector<ll>> &arr,ll i,ll visited[],ll temp[],ll count[]){
//     visited[i]=1;
//     ll flag=0;
//     for(ll j=0;j<arr[i].size();j++){
//         if(visited[arr[i][j]]==0){
//         dfs(arr,arr[i][j],visited,temp,count);
//         temp[arr[i][j]]+=count[i];
//         flag=1;
//         }
//     }
//     if(flag==1){
//         count[i]=0;
//     }

// void computeLPS(string pat, ll M, ll* lps);

// // Prlls occurrences of txt[] in pat[]
// void KMP(string pat, string txt)
// {
//     ll M = pat.length();
//     ll N = txt.length();

//     ll lps[M];

//     // Preprocess the pattern (calculate lps[] array)
//     computeLPS(pat, M, lps);

//     ll i = 0; // index for txt[]
//     ll j = 0; // index for pat[]
//     while (i < N) {
//         if (pat[j] == txt[i]) {
//             j++;
//             i++;
//         }

//         if (j == M) {
//             (*count)++;
//             j = lps[j - 1];
//         }

//         else if (i < N && pat[j] != txt[i]) {

//             if (j != 0)
//                 j = lps[j - 1];
//             else
//                 i = i + 1;
//         }
//     }
// }

// // Fills lps[] for given patttern pat[0..M-1]
// void computeLPS(string pat, ll M, ll* lps)
// {
//     ll len = 0;

//     lps[0] = 0; // lps[0] is always 0

//     // the loop calculates lps[i] for i = 1 to M-1
//     ll i = 1;
//     while (i < M) {
//         if (pat[i] == pat[len]) {
//             len++;
//             lps[i] = len;
//             i++;
//         }
//         else // (pat[i] != pat[len])
//         {
//             // This is tricky. Consider the example.
//             // AAACAAAA and i = 7. The idea is similar
//             // to search step.
//             if (len != 0) {
//                 len = lps[len - 1];

//                 // Also, note that we do not increment
//                 // i here
//             }
//             else // if (len == 0)
//             {
//                 lps[i] = 0;
//                 i++;
//             }
//         }
//     }
// }
// ll bits[500003];
// ll get_sum(ll bits[],ll index){
//     index+=1;
//     ll sum=0;
//     while(index>0){
//         sum+=bits[index];
//         index-=(index & -(index));
//     }
//     return sum;
// }

// void update(ll bits[],ll n,ll index,ll val){
//     index+=1;
//     while(index<=n){
//         bits[index]+=val;
//         index+=(index & (-index));
//     }
// }

main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);

    ll n;
    cin>>n;
    ll arr[n+1];
    for(ll i=1;i<=n;i++)
        cin>>arr[i];
    ll crr[1001][1001];
    ll trr[1001];
    trr[0]=0;
    for(ll i=1;i<=n;i++){
        trr[i]=trr[i-1]^arr[i];
    }
    for(ll i=1;i<=n;i++)
        crr[i][i]=arr[i];
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n-i;j++){
            crr[j][j+i]=max(crr[j][j+i-1],crr[j+1][j+i]);
            if((i+1)%4==0){
                crr[j][j+i]=max(crr[j][j+i],trr[j-1]^trr[j+i]);
            }
            else if((i+1)%4==1){
                crr[j][j+i]=max(crr[j][j+i],arr[j+i]^arr[j+i]);
            }
            else if((i+1)%4==2){
                if((i+1)!=2)
                crr[j][j+i]=max(crr[j][j+i],arr[j]^arr[j+i]^arr[j+1]^arr[j+i-1]);
                else
                crr[j][j+i]=max(crr[j][j+i],arr[j]^arr[j+i]);
            }
            else{
                if((i+1)!=3)
                    crr[j][j+i]=max(crr[j][j+i],arr[j]^arr[j+2]^arr[j+i]^arr[j+i-2]);
                else
                    crr[j][j+i]=max(crr[j][j+i],arr[j]^arr[j+i]);
            }
        }
    }
    ll q;
    cin>>q;
    for(ll i=0;i<q;i++){
        ll a,b;
        cin>>a>>b;
        cout<<crr[a][b]<<"\n";
    }

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 10000000007
bool compare1(pair<ll,ll> a1,pair<ll,ll> a2){
    double aa=((double)a1.first)/a1.second;
    double bb=((double)a2.first)/a2.second;
    return aa>bb;
}
// struct compare2{
//  bool operator()(pair a1,pair a2){
//      return a1.second>a2.second;
//  }
// };
// void rrrr(vector<vector<vector<ll>>> &arr){
//  cout<<arr[39][19][5]<<"--";
// }
// ll arr[500001];
// void update(ll arr[],ll qa,ll qe,ll sa,ll se,ll si,ll val){
//  if(qa<=sa && se<=qe){
//      arr[si]=val;
//      return;
//  }
//  if(qa>se || sa>qe)
//      return;
//  ll mid=(se+sa)/2;
//  update(arr,qa,qe,sa,mid,2*si+1,val);
//  update(arr,qa,qe,mid+1,se,2*si+2,val);
//  arr[si]=max(arr[2*si+1],arr[2*si+2]);

// }
// ll get_sum(ll arr[],ll qa,ll qe,ll sa,ll se,ll si){
//  if(qa<=sa && se<=qe){
//      return arr[si];
//  }
//  if(qa>se || sa>qe)
//      return 0;
//  ll mid=(se+sa)/2;
//  ll k1=get_sum(arr,qa,qe,sa,mid,2*si+1);
//  ll k2=get_sum(arr,qa,qe,mid+1,se,2*si+2);
//  return max(k1,k2);

// }

// long long max(long long a,long long b){
//  if(a<b)
//      return b;
//  return a;
// }
// ll convert(string a){
//  ll k=0;
//  ll t=1;
//  for(ll i=0;i<a.length();i++){
//      k=(k+(a[i]-'a'+1)*t)%10000000000009ll;
//      t=(t*31)%10000000000009ll;
//  }
//  return k;
// }
// ll convert(string a){
//  ll k=0;
//  ll t=1;
//  for(ll i=0;i<a.length();i++){
//      k=(k+(a[i]-'a'+1)*t)%10000000009ll;
//      t=(t*31)%10000000009ll;
//  }
//  return k;
// }
double min(double a,double b){
    if(a>b)
        return b;
    return a;
}

ll max(ll a,ll b){
    if(a>b)
        return a;
    return b;
}
ll hashs(string a){
 ll k=27;
 ll value=0;
 for(ll i=0;i<a.length();i++){
     value=((value*k+(a[i]-'a'+1)))%10000000000007;

 }
 return value;
}
ll find(ll parent[],ll i){
    if(parent[i]!=i){
        parent[i]=find(parent,parent[i]);
    }
    return parent[i];
}
void unions(ll parent[],ll i,ll j){
    ll k=find(parent,i);
    ll l=find(parent,j);
    parent[k]=l;
}

ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

// Function to return LCM of two numbers
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

// void dfs(vector<vector<ll>> &arr,ll i,ll visited[],ll temp[],ll count[]){
//     visited[i]=1;
//     ll flag=0;
//     for(ll j=0;j<arr[i].size();j++){
//         if(visited[arr[i][j]]==0){
//         dfs(arr,arr[i][j],visited,temp,count);
//         temp[arr[i][j]]+=count[i];
//         flag=1;
//         }
//     }
//     if(flag==1){
//         count[i]=0;
//     }

// void computeLPS(string pat, ll M, ll* lps);

// // Prlls occurrences of txt[] in pat[]
// void KMP(string pat, string txt)
// {
//     ll M = pat.length();
//     ll N = txt.length();

//     ll lps[M];

//     // Preprocess the pattern (calculate lps[] array)
//     computeLPS(pat, M, lps);

//     ll i = 0; // index for txt[]
//     ll j = 0; // index for pat[]
//     while (i < N) {
//         if (pat[j] == txt[i]) {
//             j++;
//             i++;
//         }

//         if (j == M) {
//             (*count)++;
//             j = lps[j - 1];
//         }

//         else if (i < N && pat[j] != txt[i]) {

//             if (j != 0)
//                 j = lps[j - 1];
//             else
//                 i = i + 1;
//         }
//     }
// }

// // Fills lps[] for given patttern pat[0..M-1]
// void computeLPS(string pat, ll M, ll* lps)
// {
//     ll len = 0;

//     lps[0] = 0; // lps[0] is always 0

//     // the loop calculates lps[i] for i = 1 to M-1
//     ll i = 1;
//     while (i < M) {
//         if (pat[i] == pat[len]) {
//             len++;
//             lps[i] = len;
//             i++;
//         }
//         else // (pat[i] != pat[len])
//         {
//             // This is tricky. Consider the example.
//             // AAACAAAA and i = 7. The idea is similar
//             // to search step.
//             if (len != 0) {
//                 len = lps[len - 1];

//                 // Also, note that we do not increment
//                 // i here
//             }
//             else // if (len == 0)
//             {
//                 lps[i] = 0;
//                 i++;
//             }
//         }
//     }
// }
// ll bits[500003];
// ll get_sum(ll bits[],ll index){
//     index+=1;
//     ll sum=0;
//     while(index>0){
//         sum+=bits[index];
//         index-=(index & -(index));
//     }
//     return sum;
// }

// void update(ll bits[],ll n,ll index,ll val){
//     index+=1;
//     while(index<=n){
//         bits[index]+=val;
//         index+=(index & (-index));
//     }
// }

main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);

    ll n;
    cin>>n;
    ll arr[n+1];
    for(ll i=1;i<=n;i++)
        cin>>arr[i];
    ll crr[1001][1001];
    ll trr[1001];
    trr[0]=0;
    for(ll i=1;i<=n;i++){
        trr[i]=trr[i-1]^arr[i];
    }
    for(ll i=1;i<=n;i++)
        crr[i][i]=arr[i];
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n-i;j++){
            crr[j][j+i]=max(crr[j][j+i-1],crr[j+1][j+i]);
            if((i+1)%4==0){
                crr[j][j+i]=max(crr[j][j+i],trr[j-1]^trr[j+i]);
            }
            else if((i+1)%4==1){
                crr[j][j+i]=max(crr[j][j+i],arr[j+i]^arr[j]);
            }
            else if((i+1)%4==2){
                if((i+1)!=2)
                crr[j][j+i]=max(crr[j][j+i],arr[j]^arr[j+i]^arr[j+1]^arr[j+i-1]);
                else
                crr[j][j+i]=max(crr[j][j+i],arr[j]^arr[j+i]);
            }
            else{
                if((i+1)!=3)
                    crr[j][j+i]=max(crr[j][j+i],arr[j]^arr[j+2]^arr[j+i]^arr[j+i-2]);
                else
                    crr[j][j+i]=max(crr[j][j+i],arr[j]^arr[j+i]);
            }
        }
    }
    ll q;
    cin>>q;
    for(ll i=0;i<q;i++){
        ll a,b;
        cin>>a>>b;
        cout<<crr[a][b]<<"\n";
    }

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 10000000007
bool compare1(pair<ll,ll> a1,pair<ll,ll> a2){
    double aa=((double)a1.first)/a1.second;
    double bb=((double)a2.first)/a2.second;
    return aa>bb;
}
// struct compare2{
//  bool operator()(pair a1,pair a2){
//      return a1.second>a2.second;
//  }
// };
// void rrrr(vector<vector<vector<ll>>> &arr){
//  cout<<arr[39][19][5]<<"--";
// }
// ll arr[500001];
// void update(ll arr[],ll qa,ll qe,ll sa,ll se,ll si,ll val){
//  if(qa<=sa && se<=qe){
//      arr[si]=val;
//      return;
//  }
//  if(qa>se || sa>qe)
//      return;
//  ll mid=(se+sa)/2;
//  update(arr,qa,qe,sa,mid,2*si+1,val);
//  update(arr,qa,qe,mid+1,se,2*si+2,val);
//  arr[si]=max(arr[2*si+1],arr[2*si+2]);

// }
// ll get_sum(ll arr[],ll qa,ll qe,ll sa,ll se,ll si){
//  if(qa<=sa && se<=qe){
//      return arr[si];
//  }
//  if(qa>se || sa>qe)
//      return 0;
//  ll mid=(se+sa)/2;
//  ll k1=get_sum(arr,qa,qe,sa,mid,2*si+1);
//  ll k2=get_sum(arr,qa,qe,mid+1,se,2*si+2);
//  return max(k1,k2);

// }

// long long max(long long a,long long b){
//  if(a<b)
//      return b;
//  return a;
// }
// ll convert(string a){
//  ll k=0;
//  ll t=1;
//  for(ll i=0;i<a.length();i++){
//      k=(k+(a[i]-'a'+1)*t)%10000000000009ll;
//      t=(t*31)%10000000000009ll;
//  }
//  return k;
// }
// ll convert(string a){
//  ll k=0;
//  ll t=1;
//  for(ll i=0;i<a.length();i++){
//      k=(k+(a[i]-'a'+1)*t)%10000000009ll;
//      t=(t*31)%10000000009ll;
//  }
//  return k;
// }
double min(double a,double b){
    if(a>b)
        return b;
    return a;
}

ll max(ll a,ll b){
    if(a>b)
        return a;
    return b;
}
ll hashs(string a){
 ll k=27;
 ll value=0;
 for(ll i=0;i<a.length();i++){
     value=((value*k+(a[i]-'a'+1)))%10000000000007;

 }
 return value;
}
ll find(ll parent[],ll i){
    if(parent[i]!=i){
        parent[i]=find(parent,parent[i]);
    }
    return parent[i];
}
void unions(ll parent[],ll i,ll j){
    ll k=find(parent,i);
    ll l=find(parent,j);
    parent[k]=l;
}

ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

// Function to return LCM of two numbers
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

// void dfs(vector<vector<ll>> &arr,ll i,ll visited[],ll temp[],ll count[]){
//     visited[i]=1;
//     ll flag=0;
//     for(ll j=0;j<arr[i].size();j++){
//         if(visited[arr[i][j]]==0){
//         dfs(arr,arr[i][j],visited,temp,count);
//         temp[arr[i][j]]+=count[i];
//         flag=1;
//         }
//     }
//     if(flag==1){
//         count[i]=0;
//     }

// void computeLPS(string pat, ll M, ll* lps);

// // Prlls occurrences of txt[] in pat[]
// void KMP(string pat, string txt)
// {
//     ll M = pat.length();
//     ll N = txt.length();

//     ll lps[M];

//     // Preprocess the pattern (calculate lps[] array)
//     computeLPS(pat, M, lps);

//     ll i = 0; // index for txt[]
//     ll j = 0; // index for pat[]
//     while (i < N) {
//         if (pat[j] == txt[i]) {
//             j++;
//             i++;
//         }

//         if (j == M) {
//             (*count)++;
//             j = lps[j - 1];
//         }

//         else if (i < N && pat[j] != txt[i]) {

//             if (j != 0)
//                 j = lps[j - 1];
//             else
//                 i = i + 1;
//         }
//     }
// }

// // Fills lps[] for given patttern pat[0..M-1]
// void computeLPS(string pat, ll M, ll* lps)
// {
//     ll len = 0;

//     lps[0] = 0; // lps[0] is always 0

//     // the loop calculates lps[i] for i = 1 to M-1
//     ll i = 1;
//     while (i < M) {
//         if (pat[i] == pat[len]) {
//             len++;
//             lps[i] = len;
//             i++;
//         }
//         else // (pat[i] != pat[len])
//         {
//             // This is tricky. Consider the example.
//             // AAACAAAA and i = 7. The idea is similar
//             // to search step.
//             if (len != 0) {
//                 len = lps[len - 1];

//                 // Also, note that we do not increment
//                 // i here
//             }
//             else // if (len == 0)
//             {
//                 lps[i] = 0;
//                 i++;
//             }
//         }
//     }
// }
// ll bits[500003];
// ll get_sum(ll bits[],ll index){
//     index+=1;
//     ll sum=0;
//     while(index>0){
//         sum+=bits[index];
//         index-=(index & -(index));
//     }
//     return sum;
// }

// void update(ll bits[],ll n,ll index,ll val){
//     index+=1;
//     while(index<=n){
//         bits[index]+=val;
//         index+=(index & (-index));
//     }
// }

    ll crr[1001][1001];
    ll trr[1001];
    ll dp[1001][1001];
main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);

    ll n;
    cin>>n;
    ll arr[n+1];
    for(ll i=1;i<=n;i++)
        cin>>arr[i];

    trr[0]=0;
    for(ll i=1;i<=n;i++){
        trr[i]=trr[i-1]^arr[i];
    }
    for(ll i=1;i<=n;i++){
        crr[i][i]=arr[i];
        dp[i][i]=arr[i];
    }
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n-i;j++){
            dp[j][j+i]=(dp[j][j+i-1]^dp[j+1][j+i]);
        }
    }
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n-i;j++){
            crr[j][j+i]=max(max(crr[j][j+i-1],crr[j+1][j+i]),dp[j][j+i]);

        }
    }
    ll q;
    cin>>q;
    for(ll i=0;i<q;i++){
        ll a,b;
        cin>>a>>b;
        cout<<crr[a][b]<<"\n";
    }

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 10000000007
bool compare1(pair<ll,ll> a1,pair<ll,ll> a2){
    double aa=((double)a1.first)/a1.second;
    double bb=((double)a2.first)/a2.second;
    return aa>bb;
}
// struct compare2{
//  bool operator()(pair a1,pair a2){
//      return a1.second>a2.second;
//  }
// };
// void rrrr(vector<vector<vector<ll>>> &arr){
//  cout<<arr[39][19][5]<<"--";
// }
// ll arr[500001];
// void update(ll arr[],ll qa,ll qe,ll sa,ll se,ll si,ll val){
//  if(qa<=sa && se<=qe){
//      arr[si]=val;
//      return;
//  }
//  if(qa>se || sa>qe)
//      return;
//  ll mid=(se+sa)/2;
//  update(arr,qa,qe,sa,mid,2*si+1,val);
//  update(arr,qa,qe,mid+1,se,2*si+2,val);
//  arr[si]=max(arr[2*si+1],arr[2*si+2]);

// }
// ll get_sum(ll arr[],ll qa,ll qe,ll sa,ll se,ll si){
//  if(qa<=sa && se<=qe){
//      return arr[si];
//  }
//  if(qa>se || sa>qe)
//      return 0;
//  ll mid=(se+sa)/2;
//  ll k1=get_sum(arr,qa,qe,sa,mid,2*si+1);
//  ll k2=get_sum(arr,qa,qe,mid+1,se,2*si+2);
//  return max(k1,k2);

// }

// long long max(long long a,long long b){
//  if(a<b)
//      return b;
//  return a;
// }
// ll convert(string a){
//  ll k=0;
//  ll t=1;
//  for(ll i=0;i<a.length();i++){
//      k=(k+(a[i]-'a'+1)*t)%10000000000009ll;
//      t=(t*31)%10000000000009ll;
//  }
//  return k;
// }
// ll convert(string a){
//  ll k=0;
//  ll t=1;
//  for(ll i=0;i<a.length();i++){
//      k=(k+(a[i]-'a'+1)*t)%10000000009ll;
//      t=(t*31)%10000000009ll;
//  }
//  return k;
// }
double min(double a,double b){
    if(a>b)
        return b;
    return a;
}

ll max(ll a,ll b){
    if(a>b)
        return a;
    return b;
}
ll hashs(string a){
 ll k=27;
 ll value=0;
 for(ll i=0;i<a.length();i++){
     value=((value*k+(a[i]-'a'+1)))%10000000000007;

 }
 return value;
}
ll find(ll parent[],ll i){
    if(parent[i]!=i){
        parent[i]=find(parent,parent[i]);
    }
    return parent[i];
}
void unions(ll parent[],ll i,ll j){
    ll k=find(parent,i);
    ll l=find(parent,j);
    parent[k]=l;
}

ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

// Function to return LCM of two numbers
ll lcm(ll a, ll b)
{
    return (a*b)/gcd(a, b);
}

// void dfs(vector<vector<ll>> &arr,ll i,ll visited[],ll temp[],ll count[]){
//     visited[i]=1;
//     ll flag=0;
//     for(ll j=0;j<arr[i].size();j++){
//         if(visited[arr[i][j]]==0){
//         dfs(arr,arr[i][j],visited,temp,count);
//         temp[arr[i][j]]+=count[i];
//         flag=1;
//         }
//     }
//     if(flag==1){
//         count[i]=0;
//     }

// void computeLPS(string pat, ll M, ll* lps);

// // Prlls occurrences of txt[] in pat[]
// void KMP(string pat, string txt)
// {
//     ll M = pat.length();
//     ll N = txt.length();

//     ll lps[M];

//     // Preprocess the pattern (calculate lps[] array)
//     computeLPS(pat, M, lps);

//     ll i = 0; // index for txt[]
//     ll j = 0; // index for pat[]
//     while (i < N) {
//         if (pat[j] == txt[i]) {
//             j++;
//             i++;
//         }

//         if (j == M) {
//             (*count)++;
//             j = lps[j - 1];
//         }

//         else if (i < N && pat[j] != txt[i]) {

//             if (j != 0)
//                 j = lps[j - 1];
//             else
//                 i = i + 1;
//         }
//     }
// }

// // Fills lps[] for given patttern pat[0..M-1]
// void computeLPS(string pat, ll M, ll* lps)
// {
//     ll len = 0;

//     lps[0] = 0; // lps[0] is always 0

//     // the loop calculates lps[i] for i = 1 to M-1
//     ll i = 1;
//     while (i < M) {
//         if (pat[i] == pat[len]) {
//             len++;
//             lps[i] = len;
//             i++;
//         }
//         else // (pat[i] != pat[len])
//         {
//             // This is tricky. Consider the example.
//             // AAACAAAA and i = 7. The idea is similar
//             // to search step.
//             if (len != 0) {
//                 len = lps[len - 1];

//                 // Also, note that we do not increment
//                 // i here
//             }
//             else // if (len == 0)
//             {
//                 lps[i] = 0;
//                 i++;
//             }
//         }
//     }
// }
// ll bits[500003];
// ll get_sum(ll bits[],ll index){
//     index+=1;
//     ll sum=0;
//     while(index>0){
//         sum+=bits[index];
//         index-=(index & -(index));
//     }
//     return sum;
// }

// void update(ll bits[],ll n,ll index,ll val){
//     index+=1;
//     while(index<=n){
//         bits[index]+=val;
//         index+=(index & (-index));
//     }
// }

    ll crr[5001][5001];
    ll trr[5001];
    ll dp[5001][5001];
main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);

    ll n;
    cin>>n;
    ll arr[n+1];
    for(ll i=1;i<=n;i++)
        cin>>arr[i];

    trr[0]=0;
    for(ll i=1;i<=n;i++){
        trr[i]=trr[i-1]^arr[i];
    }
    for(ll i=1;i<=n;i++){
        crr[i][i]=arr[i];
        dp[i][i]=arr[i];
    }
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n-i;j++){
            dp[j][j+i]=(dp[j][j+i-1]^dp[j+1][j+i]);
        }
    }
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n-i;j++){
            crr[j][j+i]=max(max(crr[j][j+i-1],crr[j+1][j+i]),dp[j][j+i]);

        }
    }
    ll q;
    cin>>q;
    for(ll i=0;i<q;i++){
        ll a,b;
        cin>>a>>b;
        cout<<crr[a][b]<<"\n";
    }

}


#include <bits/stdc++.h>
using namespace std ;
#define ll              long long int
// #define cini(x)	        scanf("%lld",&x)
// #define cins(s)	        scanf("%s",s)
#define deb(x)          cout << x << "  " ;
#define out(x)          cout << x << endl ;
#define endl            '\n'
#define pb              push_back
#define mk              make_pair
#define fastio          ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define loop(x,n,v)     for(ll v=x; v<n; v++)
#define loopr(n,x,v)     for(ll v=n-1; v>=x; v--)
#define mod             1000000007
#define all(x)          x.begin(), x.end()
#define clr(x)          memset(x, 0, sizeof(x))
#define sortall(x)      sort(all(x))
#define tr(it, a)       for(auto it = a.begin(); it != a.end(); it++)
#define PI              3.1415926535897932384626
#define setbits(x)      __builtin_popcount(x)
#define zrobits(x)      __builtin_ctz(x)
#define ps(x,y)         fixed<<setprecision(y)<<x
#define w(t)            int t; cin>>t; while(t--)
#define     vi          vector<ll>
#define     vtt         vector<pair<ll,ll>>
#define     pii         pair<ll,ll>
#define  mapp           map<ll,ll>
#define  sett           set<ll>
// int dp[1000][1000] ;
ll mpow(ll base, ll exp);
void ipgraph(int m);
void dfs(int u, int par);
const int N = 130, M = N;
//====================================
//====================================

vi g[N];
int a[N][N];
// int n;

int main(){
    fastio ;

        ll a=0, b=0, c=0, d=0, e=0, p=0, q=0, r=0 ;
        ll ans=0, res=0, cnt=0, sum=0, diff=0, mul=1, maxx=INT_MIN, minn=INT_MAX ;
        // loop(0,n,i) arr[i]=0;
        // loop(0,n,i) brr[i]=0;
        char s[100005];
        ll n,m ;
        cin >> n ;
        // cin >> m ;
        // cin >> s ;
        ll dp[n][n] ;
        loop(0,n,i) loop(0,n,j) dp[i][j]=0;
        ll arr[n], brr[n];
        loop(0,n,i) cin >> arr[i] ;

        // dp[1][0]=0;
        loop(0,n,i) dp[0][i] = arr[i] ;
        // loop(0,n,i) cout << arr[i]<<" " ;
        // out("")
        loop(1,n,i){
            // deb("")
            loop(1,n,j){
                if(i>j)
                continue ;
                else
                dp[i][j] = dp[i-1][j]^dp[i-1][j-1] ;
                // cout << dp[i][j] << " " ;
                // }
            }
            // cout << endl ;
        }

        loop(1,n,i){
            loop(1,n,j){
                if(i>j)
                continue ;
                else
                dp[i][j] = max ({ dp[i][j], dp[i-1][j], dp[i][j-1] }) ;
            }
        }

    w(t)
    {
        cin >> a >> b ;
        maxx = INT_MIN;
        loop(0,b-a+1,i) maxx = max(maxx,dp[i][b-1] );
        cout << maxx <<endl;
    }
    return 0 ;
}

// g++ x.cpp -o as && ./as


#include <bits/stdc++.h>
using namespace std ;
#define ll              long long int
// #define cini(x)	        scanf("%lld",&x)
// #define cins(s)	        scanf("%s",s)
#define deb(x)          cout << x << "  " ;
#define out(x)          cout << x << endl ;
#define endl            '\n'
#define pb              push_back
#define mk              make_pair
#define fastio          ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define loop(x,n,v)     for(ll v=x; v<n; v++)
#define loopr(n,x,v)     for(ll v=n-1; v>=x; v--)
#define mod             1000000007
#define all(x)          x.begin(), x.end()
#define clr(x)          memset(x, 0, sizeof(x))
#define sortall(x)      sort(all(x))
#define tr(it, a)       for(auto it = a.begin(); it != a.end(); it++)
#define PI              3.1415926535897932384626
#define setbits(x)      __builtin_popcount(x)
#define zrobits(x)      __builtin_ctz(x)
#define ps(x,y)         fixed<<setprecision(y)<<x
#define w(t)            int t; cin>>t; while(t--)
#define     vi          vector<ll>
#define     vtt         vector<pair<ll,ll>>
#define     pii         pair<ll,ll>
#define  mapp           map<ll,ll>
#define  sett           set<ll>
// int dp[1000][1000] ;
ll mpow(ll base, ll exp);
void ipgraph(int m);
void dfs(int u, int par);
const int N = 130, M = N;
//====================================
//====================================

vi g[N];
int a[N][N];
// int n;

int main(){
    fastio ;

        ll a=0, b=0, c=0, d=0, e=0, p=0, q=0, r=0 ;
        ll ans=0, res=0, cnt=0, sum=0, diff=0, mul=1, maxx=INT_MIN, minn=INT_MAX ;
        // loop(0,n,i) arr[i]=0;
        // loop(0,n,i) brr[i]=0;
        char s[100005];
        ll n,m ;
        cin >> n ;
        // cin >> m ;
        // cin >> s ;
        ll dp[n][n] ;
        loop(0,n,i) loop(0,n,j) dp[i][j]=0;
        ll arr[n], brr[n];
        loop(0,n,i) cin >> arr[i] ;

        // dp[1][0]=0;
        loop(0,n,i) dp[0][i] = arr[i] ;
        // loop(0,n,i) cout << arr[i]<<" " ;
        // out("")
        loop(1,n,i){
            // deb("")
            loop(1,n,j){
                if(i>j)
                continue ;
                else
                dp[i][j] = dp[i-1][j]^dp[i-1][j-1] ;
                // cout << dp[i][j] << " " ;
                // }
            }
            // cout << endl ;
        }

        loop(1,n,i){
            loop(1,n,j){
                if(i>j)
                continue ;
                else
                dp[i][j] = max ({ dp[i][j], dp[i-1][j], dp[i-1][j-1] }) ;
            }
        }

    w(t)
    {
        cin >> a >> b ;
        maxx = INT_MIN;
        maxx = max(maxx,dp[b-a][b-1] );
        cout << maxx <<endl;
    }
    return 0 ;
}

// g++ x.cpp -o as && ./as


#include <bits/stdc++.h>
using namespace std ;
#define ll              long long int
// #define cini(x)	        scanf("%lld",&x)
// #define cins(s)	        scanf("%s",s)
#define deb(x)          cout << x << "  " ;
#define out(x)          cout << x << endl ;
#define endl            '\n'
#define pb              push_back
#define mk              make_pair
#define fastio          ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define loop(x,n,v)     for(ll v=x; v<n; v++)
#define loopr(n,x,v)     for(ll v=n-1; v>=x; v--)
#define mod             1000000007
#define all(x)          x.begin(), x.end()
#define clr(x)          memset(x, 0, sizeof(x))
#define sortall(x)      sort(all(x))
#define tr(it, a)       for(auto it = a.begin(); it != a.end(); it++)
#define PI              3.1415926535897932384626
#define setbits(x)      __builtin_popcount(x)
#define zrobits(x)      __builtin_ctz(x)
#define ps(x,y)         fixed<<setprecision(y)<<x
#define w(t)            int t; cin>>t; while(t--)
#define     vi          vector<ll>
#define     vtt         vector<pair<ll,ll>>
#define     pii         pair<ll,ll>
#define  mapp           map<ll,ll>
#define  sett           set<ll>
// int dp[1000][1000] ;
ll mpow(ll base, ll exp);
void ipgraph(int m);
void dfs(int u, int par);
const int N = 130, M = N;
//====================================
//====================================

vi g[N];
int a[N][N];
// int n;

int main(){
    fastio ;

        ll a=0, b=0, c=0, d=0, e=0, p=0, q=0, r=0 ;
        ll ans=0, res=0, cnt=0, sum=0, diff=0, mul=1, maxx=INT_MIN, minn=INT_MAX ;
        // loop(0,n,i) arr[i]=0;
        // loop(0,n,i) brr[i]=0;
        char s[100005];
        ll n,m ;
        cin >> n ;
        // cin >> m ;
        // cin >> s ;
        ll dp[n][n] ;
        loop(0,n,i) loop(0,n,j) dp[i][j]=0;
        ll arr[n], brr[n];
        loop(0,n,i) cin >> arr[i] ;

        // dp[1][0]=0;
        loop(0,n,i) dp[0][i] = arr[i] ;
        // loop(0,n,i) cout << arr[i]<<" " ;
        // out("")
        loop(1,n,i){
            // deb("")
            loop(1,n,j){
                if(i>j)
                continue ;
                else
                dp[i][j] = dp[i-1][j]^dp[i-1][j-1] ;
                // cout << dp[i][j] << " " ;
                // }
            }
            // cout << endl ;
        }

    w(t)
    {
        cin >> a >> b ;
        maxx = INT_MIN;
        loop(0,b-a+1,i) maxx = max(maxx,dp[i][b-1] );
        cout << maxx <<endl;
    }
    return 0 ;
}

// g++ x.cpp -o as && ./as