/**
	Template by Akikaze (秋風) - formerly proptit_4t41.
	Code written by a random fan of momocashew and Chiho.

	H△G x Mili - November 27th, 2013
	Mag Mell (Mili) - Sep 17th, 2014
	H△G x Mili Vol.2 - May 9th, 2015
	Miracle Milk (Mili) - Oct 12th, 2016
	青色フィルム (H△G) - February 14th, 2018
	Millennium Mother (Mili) - April 25th, 2018
**/

/** -----PRAGMA----- **/
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

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

/** -----BASIC MACROES----- **/
#define endl '\n'
#define i64 int
#define ld long double
#define rsz resize
#define pub push_back
#define mp make_pair
#define fi first
#define se second
const long long MOD = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double PI = 3.141592653589793116, EPS = 1e-9, GOLD = ((1+sqrt(5))/2);
typedef vector<i64> vi;
typedef vector<ld> vd;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<i64, i64> pii;
typedef pair<i64, pii> pip;
typedef pair<pii, i64> ppi;
vi HashMod = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};

/** -----BIT CONTROLS----- **/
template<class T> int getbit(T s, int i) { return (s >> 1) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s); }

/** -----IDEAS/ALGORITHMS-----

    -------------------------- **/

/** -----CUSTOM TYPEDEFS/DEFINES----- **/

/** -----GLOBAL VARIABLES----- **/
i64 n, q, l, r; vi tree;
vector<vi> a, ans, mx;

/** -----EXTENSIVE FUNCTIONS----- **/

/** -----COMPULSORY FUNCTIONS----- **/
void VarInput() {
	ios_base::sync_with_stdio(0); cin.tie(NULL);
	cin >> n; a.rsz(n); ans.rsz(n, vi(n, 0)); a[0].rsz(n);
	for (i64 i=0; i<n; i++) cin >> a[0][i];
}

void ProSolve() {
	for (i64 i=1; i<n; i++) {
		a[i].rsz(a[i-1].size()-1);
		for (i64 j=0; j<a[i].size(); j++) a[i][j] = a[i-1][j] ^ a[i-1][j+1];
	}
	for (i64 i=0; i<n; i++) {
		for (i64 j=i; j<n; j++) {
			ans[i][j] = a[j-i][i];
		}
	}
	//for (i64 i=0; i<n; i++) cout << ans[i] << endl;
	mx.rsz(n, vi(n, 0));
	vector<multiset<i64>> Set(n);
	for (i64 i=0; i<n; i++) {
		for (i64 j=0; j<n; j++) {
			Set[j].insert(ans[i][j]);
		}
	}
	for (i64 i=0; i<n; i++) {
		for (i64 j=i; j<n; j++) {
			auto it = Set[j].end(); it--;
			if (i == j) mx[i][j] = *it;
			else mx[i][j] = max(*it, mx[i][j-1]);
		}
		for (i64 j=i; j<n; j++) Set[j].erase(Set[j].find(ans[i][j]));
	}
	//cout << "----------\n";
	//for (i64 i=0; i<n; i++) cout << mx[i] << endl;
	cin >> q; while (q--) {
		cin >> l >> r; l--; r--;
		cout << mx[l][r] << endl;
	}
}

/** -----MAIN FUNCTION----- **/
int main() {
			#ifdef Akikaze
			//freopen("FILE.INP", "r", stdin);
			//freopen("FILE.OUT", "w", stdout);
			#endif
	VarInput();
			#ifdef Akikaze
			auto TIME1 = chrono::steady_clock::now();
			#endif
	ProSolve();
			#ifdef Akikaze
			auto TIME2 = chrono::steady_clock::now();
			auto DIFF = TIME2 - TIME1;
			cout << "\n\nTime elapsed: " << chrono::duration<double>(DIFF).count() << " seconds.";
			#endif
	return 0;
}

/**
KKKKWWWWK;:.....:;,,,,,,,::,,,,,,,::,,,,,,i;,;ttttttt;:....:;EEDLffjfLGKWWWKLjjjfDWWWWWDffjffffi::::
WWWWWWWWW;:.....:;,,,,,,,::,,,,,,,:::,,,,,;;,,ttttttt,:....:,GDDGLLffLGDWWWKLjttjGWWWWWDfjjjjjji:...
WWWWWKWWW;:.....:,,,,,,,,::,,,,,,,:::,,,,,;;,;ttttttt,:....:,LGGGGGLLLLDWWWKftttjGWWWWWGjjjjjjj;:...
WWWWKKKWW;:.....:,,,,,,,,::,,,,,,,:.:,,,,,;;,,ttttttt,:....:,DDGGGGLLLLDWWWKfttttGWWWWWGjtjjjjj;:...
WWWWWWWWWi:.....:,,,,,,,,::,,,,,,,:::,,,,,;;,,ttttttt,......,EDGGGLLLLLDWWWKjtittGWWWWWLjtttttt;:...
WWWWWWWWWi:.....:,,,,,,,,::,,:,,,,:::,,,,,;;,;ttiiiit,......,DDLLLLLfLLDWWWKjiiitGWWWWWLttttttt;:...
WWWWWWWWWi:.....:::::::::::::::::::.:::::::,,,,,,;;;;,:.....,fLfffffjffGKKWEjiiitLWWWWWLttttttt;:...
WWWWWWKEDi:.....:::.::.:.:.......:..::::::::::::::::::......:,,,,,,,,,,;;;;;,,,,,,;;;;;;,,,;;;;,:...
KKEEKKEDG;:......::,,::,,:::::::::::::::::::.:::::::::......:::::::::::::::::::::::::::::::::::::...
jjttffLDG;:......:,ii;;ii,:,ii;;ii,:,i;,,;;::,;,,,,,:.......::,,,,,,:::,,:,,:::,,:,,,::::,::,::::...
ii;iiitLf;:......:,it;;it;:,it;;tt,:,tt;;ti,:;i;;it;:.......:,ii;;ii,:;ti;it;::;;;;ti,:;i;;ii,::....
iiiiiijft,:......:,ii,,;i,:,ii;;ii,:,ii;;ii,:;i;;;i;:.......:,ii;;ii,:,t;;it;::;;;;ti::;i;;ii,::....
tttttjffi,.......:,;;,,;;,:,;;;,;;,:,;;,,;;::,;,,;;,:........,;;,,i;,:,;;,;i,::;;,;i;::;;;,;i,:.....
ffjjffLft;:......:,i;;,;i,:,ii;;ii,:,ii;,;;::,;;,;;,:........,;;,,;;::,;,,;;,::;;,,i;::;;;,;;,:.....
LLLLLGGGf;:......:,ii;;it;:,tt;;tt,:,ii;;ii::;i;;;i;:........,ii;;i;::,i;;ii;:,ii;;ti::;i;;ii,:.....
DGGGGDDDLi:......::;;,,;;,::ii;;ii,:,i;,;i;::;i;;;i;:........,ii;;ii::;i;;;t;:,ti;;ti::;t;;tt,:.....
EEEEDDEDGt:......::::::::::::::::::::,:::,:::,,,,,,,::.......:,,,,,,:::,,,,;,::;,,,;;::,;,,;;,:.....
EEEEDDEEDj:....................................::::::........:::::::::::::::::::::::::::::::::::....
EEEEDDDEEG;:......................................................::................................
EEKEEEEEEGj:..............................................:.........................................
KKKKKKKKKEL;:............................................::.........................................
WWWWWWWKKKDj:.........................................:::::...::::,:............................::::
WWWWWWWWWKEG,:............::::................:jfLffLLj::,,::::.::,:.......................:::::,,;i
KWWWWWWWWWKD;::::,,,,,,,,,;;;,;::::::::..::.;tffLjffDGjfttL,,:::::,,,:.............:.......:,,,,;;ii
KKWWWWWWWWft,:::,tGGGGGGLGGfji;;tfttttfffLL;ittjjjLLGLGffEjttji,,,::;i,,,,,;;;;;;;,;,,,,;;,,,;;;;;;i
KKKKKKKWWD;:::..:;DGDEEEEEEGji,;;iitjittLttit;tjjfDGGfLfGfGffjfjfit,;;;itLDEDDDGGGGGjiitti;;itji;;;i
KKKEDEEKWK;:....:;GGDKWWKKELLji;;iiti;;;tjjjLtfjjtfjLLLLGffLLLLfLLj;;,,;;tDEDDDDGGGLt;;tii;;iitiitit
WWKEDDEKWWG,:...:;DKDEW#WEjt;;i;ii;;;;;,GLDDGDDLGEitDjLLGGGLDGfEELfGi,,i;;tjLGDGLGEjiiitttiitttiiitj
WWWKKKEEKKWj::.::iDEDDEWWDii;;;;;;,,;;,,LDEGEKEDELGLfGLfLLLfDGKWDELGLjii;;;;tGGLfGEjiiiiiittti;;;;it
WWKKKEDDDKKKt;,;iLGGGLLLGfiii;i;,;i;ii;DDKDLKEEEKKDKfLjEfELDGEKELEKEEtti;;i;tffffLGLt;;ttttjt;;iittj
WWKKEDGGGDEEEDDDGfjtttiii;;,,;i;,ii;;itEKKKKWKKKKKKEEELtLfDGKWDEWKKLLGDj,;iijLfjjjfLfitjtttjtittjfff
KEEDGLffLGGGGGLfjti;;;ii;;,;,;iii;,iiiDWKKKKWWW#WWWKKEEDKGDfWfKWWEGGGjiti;iitjtttttttittiittttjjjttf
EDDGLfjjfLLfffjtti;;;;tjt;;;,;;;;;,,;;KWWKEWWWWWWKEKKEKDEEDWEKWKEDDDEG,,iii;;iiiiii;;;iii;;;iitttttL
KKEEDGjitffLLLffjti;;itjj;;,,;,;,;,,;DKWWWWWWWWWWWKWWEWKKW#EEKKKEGWWEj;:,i;,,;iti;;;iiiiii;;iitiijjD
WWWDLfjiiitfDGfLLffttjjji,,,;;;;;,,,;KWWKWWWWWWWWKWWWWWKWWWWKKKEWWWKfii,:;;;;tti;,,tGGGLGGLLLfjjjfDD
WWDfjtiii;;;;;,;;iitfLj;,,:,i;i;ti;;jWWWWWWW#W#WWWWWWWWWWWWEWWGW#KWGjti,:,,,,iti;,,fWEjtjLLjfLjjjji;
WDGfji;;iii;;,:,,,,,;;;;;,,;;ii;;i;;KWWWWWWW#W#WKWWW#W###WGWE#WWKWKfti;,,:,;;;ff;;;fWKfjt;;ijfjjfi,:
EGGGfjiitiii;,,,,,,,;;;ti;;;iii;;t;iWWWWWWWW#WKKWWWWWW###WW#WWWWKELjti;;;,,,,,;;;;;jGGjt;;itjjtit,:.
LEEGGLjjji;;;;,,,,;;iitttt;;tti;;tfGW#WW#WKWWWK#WW##W###WWWWW#WWKELtii;;;;,,,,;;;,;fji,;iii;iti,;,:.
fWDLGLfLjii;;;,,,;;;;;;;iii;iji;itfEWWWWW#WW#W##WW#W######W#WWWKGLjiii;;;;i;;;;ti;;ti;:,;i;,;;;:,,:.
fWEDGLffjjti;;,:,,,,,,;iii;;tjt;itfKWWWWWW##WW#WW###W##W###W#WELjtii;;;,;;ii;,,i,,ij;,,,;i;;;,,::...
fWWKGtttii;,;i,,;i;,;iit;tf;;tfiitjWWW##W#WW#WW##WWW########WKftitii;;;;t;tt;,,,::;,:,;;ii;;;i;:....
fWKKLtiii;,,;ti;tjtitii;,,;;tjftiiEWWWWWWW##WWW##W##K##W###WWEtii;;;;ffDKjfji;,,:;;;;;;itttiijt,:...
LWKEjttiii;ijjfGLjttti;;;;;iifLt;;EWW#W#WWWKWK#WW###WW#####WKGiiiiitjfffGjjti;;;;t;,;;ttijiiijti;:..
LKKEtittjLjjGDDEEGji;;;;;iittGGf;iKWWWWWWEEWW#WWWW#KKKW###WWKfii;itLGLjiffj;;;,;;ii;,,jjjiijt,,;,,:.
LKKDjijLGLfLDKKftjt,,::,;;;LffLGjKKWWWWWWKWWWW#WWW#WWKWW##WEGjii;;iLDKEtjjt;t;;,,;iittLLDjtLfi,,:::.
LKEELffLGLLDDEj,,;tt;::,ittfGfjDGDWWKWW#KWKW##WW###WWEEKWWKDft;i;;;tGK#Eitiii;,:,;LfjfLtLLtLjjti,:..
GKEKDGLGGGDEEDii;;itti;;jtijLfjtGKWWWWWKWEWW#####W#WWGDEKKEDfi;;;;,,;fGt;i;;i;,,ittjjtttjGGELLti;:..
GEEDGfGGLffLLiiji;;;;tjtfjjffLfjGWWWWWWWWWWW#######GKDDDEEEDtti;;;,,,tt;,i,;jfttij;jfitfjLGEEf;;,:..
GEEDLjLDDjtfi,ijt;iitff;tDDLfGLEGWW#WWWWW#######W##EGEDGDEEGjiii;i;;,,,,,,,;jGt;ttfjjjifDDfLEE;::,::
LEEDLjffjfGGiifGDjfttLDitffjfGjDDKWWWWKWW##W#W#####KGLGGDGDLttiiiiii;,,;;:;tit,,tiii;iiLGGGtLEi:.:,:
GEEELjfttfGLfjfGDLGGGKKfLfLGLLjGKKWWWWWW###########DtfGGLEGfjttttttti;,;;:ii;,,;;i,i;iiLLj;;fft:.::.
GEEGGLLLfjjfGELLLfGGGLEt;;tjGGLEW##WWWWW##W########WWEGLLLftttttttjti;,t;,.jt;iiiiijfitjjt;,;tt;,:,,
DDLGGDEDLjjjGEftjLEfffi,,,;tjjEWWWWWWW##WW#########WWWELLjjttittttttti;i;ttjf;ititffjitjfj;::;itiitj
DGLGEEKDjtjLGjttjfLLGj,,;i;iiiDWWWKKWW##W##########WWWELfjttiittttjtttGjjKji,,fGjjfLj;tLft;::,,ittjL
GDGDEKKLttfKDjtLDfLDDi,itttjiiEKWWWWW##############WWKELfjtttitttttjjttEftit;;DDLjfLjijjtti,::,;jjLG
GEDDEKEGjGWWKEKWWEEEEfjfLjttjLKKWKWWW##############WKKEGfjtttttttjjjjt;jfj;tjLEELjfffttiti;;::,,;fDE
GEDDEKKGjLKWW#WWKDGEEEDGDLjtjDKWWKWW###############WKKEDLjjttttjjjjffjt;LjtjfGDGGfLtLjffLjit;:,,,jEE
LEDDEEKGjjLKW###EttDEEGfjLDLEEKWWW##W###W##########WEEEGLfjjjjjtjjfffDDLjjftLLLftfjiLDEDffftj;i;,iDK
LEDEEEEDjtfEW#WWLtjEEGfjjfEKEEWWWWW################WWEEGGLjjjjjjjffLKDEGttjjfjttijfDGLLfjft,;ttt;;fD
LEDEKEEDffGWW#WWDfGELLffLfKEEWWWWW#################KDEDDLLfjjjjjjffLfLLftttjtjfGjifEELfjjj,.:,;ittjj
GEEKKWKEGDKWWW#WWEEELGfjLEEEEWWWW#################WKDDDDGLLfjjjjfLLfjLLjjjjfjjLEDjEWELfjjj;::,,ijfjj
DEEKWWWKEEWWWWWWWWWWEGjfGEEKKWWWW#################WELGEDGGLffjjffLLffjjtjLDLLLfEEjKWELGjfjti;itffffj
EEEKWW#WKKWWWWWWWWW#KLLEDDEKWWWWW##############W#WEELGDDGGLffjfffLfffttttfDGGGLEKDWWKEDitfLLLGGGGLLL
DDEKWW#WKKWWWWWWWWWWEGEKEEKEKWWW#################WGGLGGDGGLfjfffffffji;tjfLGDEEEWKDEGLGijGEEEDGLLLff
GDEKWW#WWKKKKKKKWWWWDEKKEEDLWWW########W########WEDLLLGDGGLGEGtjfffLfi;;;tGDDKEDKLtfDLftttfGfjiii;;;
GDKKWW#WWKKKWKDEKKWWKKKKKKDEWWW##W##############KELLLLGDGGGELjLEjjjfj:,tijDDDEEDEGjjDGjt;,iti;,,,,,,
EKKKWWWWWKKWWKDDKKWWWKKKWKKKWWW##WW###WW###WWW#WEGLffLLGGLEDLLDEL;tGi:,jjGGGDEDLLDGffGji;,;;;,,:,:::
WKKKWWWWWWWWWKDLGEKKWKEKWKKKWW##WWW##WWW##WWW#WEDLfffLLLLEDDEEKKGjfDLjLjiLLLDEDjfGGffGti;;;;;,,,,,,:
WKEEEEEKWWWKEEEGLDEKKEDGEKKWW###WW###WW##WWWWWDGLffffLLLDELDEEKWKEGGGLLLiijttDDLGDDGGLjttiiiiiiii;;;
WKEEt;;LWWWEDDDDDDEEGGGjLEKWW##W####WW#WWKWWWDfjjjjffLLLEEGDKEWWWEDDGGfLfit;,;GGKKEGGfGGGLfjffLLLfjj
WWKi:.:iKKKEEDEEDDDDGGfitKWWW##K###WWW#KEKWWfiiittjjfLLfGEDKKEKWKEDDDGtifftji;jDKKEDGfEKEEEEEEEEKEDD
WKt:  :jEEEEEEEEDDEDDDjtGKKWW##W###WW#GfKWL;,,,;ittjfLfEEDDEKEEKWWELGDLjffjGEfLDWWEDLLEEEEEEKKEEEKKE
Kt:. :iGDDDDDDDDDDDDGGLLKWEW##WW#WW##GDKDi....:,;ittjfKWWKKKKEEKWKKEEGGfjjLDEEGGWKDDfLDGLDKKEEGGDEEE
t:. .;LGGDGGGGGGGGGGLLGGGWWW#WW#WWWWGEj;.      .,;iitftGLKWKKKKDEKEDKKDGftLDDGLGEDLfjDEDDEWWWKDDEKKK
:. .,tLGDDGLLLLLLGGGLLGGDWW##W###WWKK:,        ...;;it;tGEKKWKKEEKEEKKDDt;tGEGfLDDLjfEWWWWW#W#WWWWWW
. .:j;iLGGLffLLLLLLGLLDGDKW######WWW.:           ...;i;;jEKKKKWWKKWWKGLDGtffGGfjGDfjGEKWWWWWWWWWWWWW
..:tGiifLLLffffLLLLLfLGLGKW#####WWW.:             ....;;;DKKKKKWKKKWKLfDKDLjfGjjGGtjLGEWKKKKKKKEDEEK
:,ifLjjLLffffffLLLLLfLLLGKW#W###WW:,               ...::tGKKKEKWKKKKKDfGEEGfLLjfGftjGDEEDEEEDEDDGDED
iitLLtjfffffffffLLLLLLGGDWWWW##WWG..               .....::KDGEKWKKWKKELLEKEjtjjffttLGGEEDDDDDDDDDDDD
Dj;tjtjffffLffjjfLLLLGGGG#######W::                 ....::EEEKKWEDEKKKDLDEDfttffLtfLLLGGGGGGGGGGGGGG
DL,;ttffjjfLffjjLLLLLGDGDW######E:                   ....:.WWKKKEDEKKKKGGDLLjtjfDLfjffLLLLLLLffffffL
DDjitjffjfjjffffLfLLLLDGGWW#####: ..                  ..:..EWKKEKKKWKKKEEDftttjfGfttjjffffffjfjfjjjj
DDjttjjfffjjfffffjfLLLDDDW#####W: ...   . .i,iit;;ft   .....KKKEKKWKWEEKKDfjLLfjfjtjjjjjjjjjjjjjjjjt
DGjtjjffffLfLfffffLLLLGDEW#####t....   .. GG.ttjLDf:,f .....;KKWWWWWWKDEDDLLDGftjjjjjjjjtttttttttttt
DLjjjfffffffffffLLGLLLGDE######: .: .  .. ,.       .jE,:.....EKWWWWWWWKELGDDEGjjjjttttttttttttititti
DfjjfLLfffffLLLLLGDGGEEEKWW###L .:... ...        .    .j......KKWWWWWKKKGDDDEGjtjtttttttiiiiiiiiiiii
DjjfLLfLLfjfGLGGGDDDDDEE#W#W##,.::....... .:...::::..   ;.....iKWWWWWKKEDDGLffttjtttiiii;;;;;;;;;;;,
LjffLLLGLfffGGDDDEDDEEKKWW###W..,.........    :,:::...:: :.....KWWKKKKKEfGDGfffjjtttii;;;;;;;,,,,,,,
LLLLGGGDDGGDEEEEEEDDKKKWWW###,.:,.....  .:  :,,:..:...:,, :.... KWKKWKWGjLDDGLLLttti;;;;,,;;;,,,,,,,
LGLLGGLGDDDEEEEKKKKKKKKW#####:.::........:..,,:::::...:,,:......tKKWWKKDfDKGjjfLttti;;;;,;;;;,,,,,,,
GGfLLLLGDEEEEEEKKKKKKKKWW####..::........::;;,::::::...:,,,......KWWWWEDLDEGjijftiiiii;i;;;i;;;;;;;;
LGffffGEKKKKWKKWWWKWKKKW####j.::::.......:;,,::::,:....:,,:;:.....WWWKKEGEEKGtfjiiiiiiiiiiiiiiiii;;;
LLfLfLEWWWWWWWW#WWWKKEWWW###:.::::::...:::,:,::,:::.....:,,;:.....EWWKKKDEKWKLLtiiiiiiiiiiii;ii;;;;;
LffGGDKWWWWWWWWWKKEEEEWWWW##..::.:::.:.:::,,:,,:::::....:,,:,:.....WWWEEEEKWWKDt;;iiii;;;;;,,;;,,,,,
LffDDDDEEDDEDDEDDEDDDDEKWWWW..::::::::.:..:,:,:,:::.....:,,,;,.....tWKEEEEKWWWDt;;iiii;;,,,:,,,:::::
LLfGGLLGGLGGDDDDDGGDGEEWWE#W.::::::::::::.,,:,,,:::......:,,;,:.....EEEKEDEWWWDi;;;iii;;,,,:,;;,::::
LfjGLLLGGLLGDDGDDLGDGEEWEEWW..:::::,,..:..,,,,,,::.......:,,:,,.....jEEKGGEWWWGt;;iiiii;;,,:,,,,::::
ffjLGGGGGLGDGLLDGLGDEEKWGE#W...::,:,:.::.::,,,,,::.....::::,,,,,.....KKEDDKWKKDj;;iiiiii;,,,,,,,::::
ffjfLLGGLLGDGLLDGLLGEKWWWKWW...::,,,:.:.::,,,:,:,:......::,:,;;,... .EEDDEDKKWEj;;iiiiiii;,,,,,,::::
LftjLLGLLGGDGLGGKGfjDWWWEEW#....:,,,::::::,,,,::::..  ..:::,,,,,:....DEDEEGGDEDfi;;iiiiii;,,:,,:::::
LfjjLGDGLGGGGGGGGEEEEWDGE#W#....::,:::.:::,,,::::.. .....:::,:,,,... GKDEEEDEKEELiiiiiiii;,:,,,:::::
GLjjLDDGGLGGLLLGLLKKEGKWWWW#i...::,:.:::::,,,::....:.::...:.,:,,,:....KDLLDEDEDDLjttttttti,,,;;,::::
GGffDDGGGGDLLLGLfLGKWWW##WW#i;...:,:::,,::,,:::,,,;,;t;: ,i::::,,,:...EDjtGDGLjffjfjjjjttt;;;i;,,:,:
DGffGDLLLGDLLLGfffLGGLW#####,t:.::,:::,,::::;:,;ii;;iitt:::i::;:,,:...LEfjGLLftjLfffjjjttttiti;,,,,:
KEGLGGLffGGLfffjjjfGfffL####;i,:::::::,:::tiiii;i,,;iiii:,;:;,f:,,,:...WKKKKKKDDEEGEfjtttttttt;,,,,,
KEEEDGffLGLjtfffffLGjtjW####;;,.:::..:::jt;;;ii;;i;;iii;:,;;,,,;:::::..WWWWWWKKEEEDEftiiiiitti;,,,,,
DDDDDGLLGGLfjLGfffLGtitjjGW#f;,::::.::,ii;i;;;i;;iiii;;,:,:,;;;W:,:::::WWWWWWWWKGEKEt;;;;;;;;;;,,,,,
GDDDEDDGGLLfLGLjtjLLtittjjjW#;;::.:.:;i;;;,;;;i;;iii;;i.,,,::,:Gj::::::WWWWWWWWWDGGDi;;;;,,,,,,,,,,,
DDEEDGGGDGGGGDLLffLftttjjttjfi;::.::;t,i;,,;,;;;;;;;;;;.,,,,;;:ED:,:,,:KKWWWWWWWWEEKt;;;;,;,,,,,,,,,
DGDEDGLGEDGGGLffffjitttjjiitji;,::::j;,i,;;;;;;;;;;;;;,.:,,:,;,.L#:,,,,WWWWWKWWWWWKEt;;;;;;;,,,,,,,,
LGDEEGLGDGLLLLjjjftitttjftitjLit,,:;j,;i,,,;;,;;,,;;;,::,;,::;,.EG,,:;,WWWWWKWWKKWEEt;;;;;;;;;;;;;,,
GGEEDGGGLfffGLfjtjffjjttjtttjfiii,,tj:ii,:,;;;;;,,,,,,.:,;;,.,,,.GW:,;,WWKWWWWWWWWKGjttttiiitttiiii;
GLLDDDDLffjfGLffjjLfjtitfjjjjLtij;,tf,t;::,,,,,,,,,,,,.,,,,,:,,::;f;;i,KWKKWWWWWWKKLijjjjjjjtttttttt
GLfGGGDLffjfLfjjjfGjtiitfjtjjLLitt:tf;i;,,,;,,,,,,,,,::,,,,,:;;:;.KG:i;WKKWWKWWWWWWGtjjjjjjttttttttt
EEGLLGDGfLfLLffjjLLtiiitfjjjjLfjij,jj;ti,,,,,,,,,,,,,.:,,,,::;;:;::L:iiWWWKWWWWWWWWKLjjjtttttttttiii
DDLffLGGLLfLLjjjjfftiiitftttjGfftttjj;i;:,,,,,,,:::,,.:,,,,,:,,:i,,E;iLWWKKWKKWWWWWKDtttttttttiiiiii
DLfjfLDLffffjttjjfftiiiijtjjfLLLjttfj;it:,,,,:,,,::,,.::,,,,:,,,ii,WtiKWWKKWKKWWWWWEGtiiiiiiiiiiiiii
DfjjjGGfjfLLjjjjfLLjjjjffffffLfjfjtffiti,,,,,,,:,:::::,:,,,,:,,:ii,LtiKWWWKKWWWWWKKKGtiiiiiiiiiiii;i
DGLLLDGfffLGLfffLLfjtttjfjjjffLjfjjtLtti:::,:,:,::::.:,,,,,,,:,:ii;f;;KKWWKKWKWWWWKKEjii;i;;;;;;;;;;
GLLLGGfjfjfLfjjjLGLftttjfjjtjfffLLjjjGtt:,,,,,,,,::: ::,,,,,::,,it;Ei;KWWWKKWKWWWWWWELi;;;;;;;;;;;;;
GGGLGLfjjjfffjjjLDGLjtttjjjjjffjfLffjLti,,,,:,,,,:::.::,,,,:,:,;tt;LiiKKKWWWWWWWWWWWKDi;;;;;;;;;;,,,
GGLLDLffjfffffffGGDGfjtjfjjjjLLLffLjfjtt,,,,,:,:,:::.:::::,:::,itt;ti;KKKWWWWWWWWWKKKEi;;;,,,,,,,,,,
LLLGGjjjffLffjjfLGGLfjjjfjjttjjffjjLfjjt,,,,,,,,,::::::::::,,:,ittDtijWKKWWWWWWWWWWKEDi;;;;;,,,,,,,,
fjLDjijfLLGLfjfLGLfjjjjjfjjjjfLGGLLDjftt;,,,,,:,,,,.:::,,,,,,:,ttt#tiWWWKWWWWWWWWWWWKEj;;;i;;;,,,,,,
jjLEfjjfjfLfjttLGLjtjtttjjtjfLLLGGGDEjftt,,,,:::::: ::::::::::,ttKKjiWWWKKWWWWWWWKWWKKDi;ii;;;;;,,,,
jjfGfffjtjjjjffLGGjtjjiifjjjfLLLGGGGELjLt,,,,,,::::.::::::,:,,,ttWELKEWWWKKKWWWWWWWWWWKj;ii;;;,,,,;;
jjjffffjjffjjffLLLftttitjjjtjfffLLGLGLjft,,,,,:::,,.::::::,,,:;tjWKE,WKWWWKKKWWWWWWWWWWjii;;;,,,,,,;
**/

// Tribute to a friend I owe countless. Thanks, TN.


/**
	Template by Akikaze (秋風) - formerly proptit_4t41.
	Code written by a random fan of momocashew and Chiho.

	H△G x Mili - November 27th, 2013
	Mag Mell (Mili) - Sep 17th, 2014
	H△G x Mili Vol.2 - May 9th, 2015
	Miracle Milk (Mili) - Oct 12th, 2016
	青色フィルム (H△G) - February 14th, 2018
	Millennium Mother (Mili) - April 25th, 2018
**/

/** -----PRAGMA----- **/
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

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

/** -----BASIC MACROES----- **/
#define endl '\n'
#define i64 long long
#define ld long double
#define rsz resize
#define pub push_back
#define mp make_pair
#define fi first
#define se second
const long long MOD = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double PI = 3.141592653589793116, EPS = 1e-9, GOLD = ((1+sqrt(5))/2);
typedef vector<i64> vi;
typedef vector<ld> vd;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<i64, i64> pii;
typedef pair<i64, pii> pip;
typedef pair<pii, i64> ppi;
vi HashMod = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};

/** -----BIT CONTROLS----- **/
template<class T> int getbit(T s, int i) { return (s >> 1) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s); }

/** -----IDEAS/ALGORITHMS-----

    -------------------------- **/

/** -----CUSTOM TYPEDEFS/DEFINES----- **/

/** -----GLOBAL VARIABLES----- **/
i64 n, q, l, r; vi tree;
vector<vi> a, ans, mx;

/** -----EXTENSIVE FUNCTIONS----- **/

/** -----COMPULSORY FUNCTIONS----- **/
void VarInput() {
	ios_base::sync_with_stdio(0); cin.tie(NULL);
	cin >> n; a.rsz(n); ans.rsz(n, vi(n, 0)); a[0].rsz(n);
	for (i64 i=0; i<n; i++) cin >> a[0][i];
}

void ProSolve() {
	for (i64 i=1; i<n; i++) {
		a[i].rsz(a[i-1].size()-1);
		for (i64 j=0; j<a[i].size(); j++) a[i][j] = a[i-1][j] ^ a[i-1][j+1];
	}
	for (i64 i=0; i<n; i++) {
		for (i64 j=i; j<n; j++) {
			ans[i][j] = a[j-i][i];
		}
	}
	//for (i64 i=0; i<n; i++) cout << ans[i] << endl;
	mx.rsz(n, vi(n, 0));
	vector<multiset<i64>> Set(n);
	for (i64 i=0; i<n; i++) {
		for (i64 j=0; j<n; j++) {
			Set[j].insert(ans[i][j]);
		}
	}
	for (i64 i=0; i<n; i++) {
		for (i64 j=i; j<n; j++) {
			auto it = Set[j].end(); it--;
			if (i == j) mx[i][j] = *it;
			else mx[i][j] = max(*it, mx[i][j-1]);
		}
		for (i64 j=i; j<n; j++) Set[j].erase(Set[j].find(ans[i][j]));
	}
	//cout << "----------\n";
	//for (i64 i=0; i<n; i++) cout << mx[i] << endl;
	cin >> q; while (q--) {
		cin >> l >> r; l--; r--;
		cout << mx[l][r] << endl;
	}
}

/** -----MAIN FUNCTION----- **/
int main() {
			#ifdef Akikaze
			//freopen("FILE.INP", "r", stdin);
			//freopen("FILE.OUT", "w", stdout);
			#endif
	VarInput();
			#ifdef Akikaze
			auto TIME1 = chrono::steady_clock::now();
			#endif
	ProSolve();
			#ifdef Akikaze
			auto TIME2 = chrono::steady_clock::now();
			auto DIFF = TIME2 - TIME1;
			cout << "\n\nTime elapsed: " << chrono::duration<double>(DIFF).count() << " seconds.";
			#endif
	return 0;
}

/**
KKKKWWWWK;:.....:;,,,,,,,::,,,,,,,::,,,,,,i;,;ttttttt;:....:;EEDLffjfLGKWWWKLjjjfDWWWWWDffjffffi::::
WWWWWWWWW;:.....:;,,,,,,,::,,,,,,,:::,,,,,;;,,ttttttt,:....:,GDDGLLffLGDWWWKLjttjGWWWWWDfjjjjjji:...
WWWWWKWWW;:.....:,,,,,,,,::,,,,,,,:::,,,,,;;,;ttttttt,:....:,LGGGGGLLLLDWWWKftttjGWWWWWGjjjjjjj;:...
WWWWKKKWW;:.....:,,,,,,,,::,,,,,,,:.:,,,,,;;,,ttttttt,:....:,DDGGGGLLLLDWWWKfttttGWWWWWGjtjjjjj;:...
WWWWWWWWWi:.....:,,,,,,,,::,,,,,,,:::,,,,,;;,,ttttttt,......,EDGGGLLLLLDWWWKjtittGWWWWWLjtttttt;:...
WWWWWWWWWi:.....:,,,,,,,,::,,:,,,,:::,,,,,;;,;ttiiiit,......,DDLLLLLfLLDWWWKjiiitGWWWWWLttttttt;:...
WWWWWWWWWi:.....:::::::::::::::::::.:::::::,,,,,,;;;;,:.....,fLfffffjffGKKWEjiiitLWWWWWLttttttt;:...
WWWWWWKEDi:.....:::.::.:.:.......:..::::::::::::::::::......:,,,,,,,,,,;;;;;,,,,,,;;;;;;,,,;;;;,:...
KKEEKKEDG;:......::,,::,,:::::::::::::::::::.:::::::::......:::::::::::::::::::::::::::::::::::::...
jjttffLDG;:......:,ii;;ii,:,ii;;ii,:,i;,,;;::,;,,,,,:.......::,,,,,,:::,,:,,:::,,:,,,::::,::,::::...
ii;iiitLf;:......:,it;;it;:,it;;tt,:,tt;;ti,:;i;;it;:.......:,ii;;ii,:;ti;it;::;;;;ti,:;i;;ii,::....
iiiiiijft,:......:,ii,,;i,:,ii;;ii,:,ii;;ii,:;i;;;i;:.......:,ii;;ii,:,t;;it;::;;;;ti::;i;;ii,::....
tttttjffi,.......:,;;,,;;,:,;;;,;;,:,;;,,;;::,;,,;;,:........,;;,,i;,:,;;,;i,::;;,;i;::;;;,;i,:.....
ffjjffLft;:......:,i;;,;i,:,ii;;ii,:,ii;,;;::,;;,;;,:........,;;,,;;::,;,,;;,::;;,,i;::;;;,;;,:.....
LLLLLGGGf;:......:,ii;;it;:,tt;;tt,:,ii;;ii::;i;;;i;:........,ii;;i;::,i;;ii;:,ii;;ti::;i;;ii,:.....
DGGGGDDDLi:......::;;,,;;,::ii;;ii,:,i;,;i;::;i;;;i;:........,ii;;ii::;i;;;t;:,ti;;ti::;t;;tt,:.....
EEEEDDEDGt:......::::::::::::::::::::,:::,:::,,,,,,,::.......:,,,,,,:::,,,,;,::;,,,;;::,;,,;;,:.....
EEEEDDEEDj:....................................::::::........:::::::::::::::::::::::::::::::::::....
EEEEDDDEEG;:......................................................::................................
EEKEEEEEEGj:..............................................:.........................................
KKKKKKKKKEL;:............................................::.........................................
WWWWWWWKKKDj:.........................................:::::...::::,:............................::::
WWWWWWWWWKEG,:............::::................:jfLffLLj::,,::::.::,:.......................:::::,,;i
KWWWWWWWWWKD;::::,,,,,,,,,;;;,;::::::::..::.;tffLjffDGjfttL,,:::::,,,:.............:.......:,,,,;;ii
KKWWWWWWWWft,:::,tGGGGGGLGGfji;;tfttttfffLL;ittjjjLLGLGffEjttji,,,::;i,,,,,;;;;;;;,;,,,,;;,,,;;;;;;i
KKKKKKKWWD;:::..:;DGDEEEEEEGji,;;iitjittLttit;tjjfDGGfLfGfGffjfjfit,;;;itLDEDDDGGGGGjiitti;;itji;;;i
KKKEDEEKWK;:....:;GGDKWWKKELLji;;iiti;;;tjjjLtfjjtfjLLLLGffLLLLfLLj;;,,;;tDEDDDDGGGLt;;tii;;iitiitit
WWKEDDEKWWG,:...:;DKDEW#WEjt;;i;ii;;;;;,GLDDGDDLGEitDjLLGGGLDGfEELfGi,,i;;tjLGDGLGEjiiitttiitttiiitj
WWWKKKEEKKWj::.::iDEDDEWWDii;;;;;;,,;;,,LDEGEKEDELGLfGLfLLLfDGKWDELGLjii;;;;tGGLfGEjiiiiiittti;;;;it
WWKKKEDDDKKKt;,;iLGGGLLLGfiii;i;,;i;ii;DDKDLKEEEKKDKfLjEfELDGEKELEKEEtti;;i;tffffLGLt;;ttttjt;;iittj
WWKKEDGGGDEEEDDDGfjtttiii;;,,;i;,ii;;itEKKKKWKKKKKKEEELtLfDGKWDEWKKLLGDj,;iijLfjjjfLfitjtttjtittjfff
KEEDGLffLGGGGGLfjti;;;ii;;,;,;iii;,iiiDWKKKKWWW#WWWKKEEDKGDfWfKWWEGGGjiti;iitjtttttttittiittttjjjttf
EDDGLfjjfLLfffjtti;;;;tjt;;;,;;;;;,,;;KWWKEWWWWWWKEKKEKDEEDWEKWKEDDDEG,,iii;;iiiiii;;;iii;;;iitttttL
KKEEDGjitffLLLffjti;;itjj;;,,;,;,;,,;DKWWWWWWWWWWWKWWEWKKW#EEKKKEGWWEj;:,i;,,;iti;;;iiiiii;;iitiijjD
WWWDLfjiiitfDGfLLffttjjji,,,;;;;;,,,;KWWKWWWWWWWWKWWWWWKWWWWKKKEWWWKfii,:;;;;tti;,,tGGGLGGLLLfjjjfDD
WWDfjtiii;;;;;,;;iitfLj;,,:,i;i;ti;;jWWWWWWW#W#WWWWWWWWWWWWEWWGW#KWGjti,:,,,,iti;,,fWEjtjLLjfLjjjji;
WDGfji;;iii;;,:,,,,,;;;;;,,;;ii;;i;;KWWWWWWW#W#WKWWW#W###WGWE#WWKWKfti;,,:,;;;ff;;;fWKfjt;;ijfjjfi,:
EGGGfjiitiii;,,,,,,,;;;ti;;;iii;;t;iWWWWWWWW#WKKWWWWWW###WW#WWWWKELjti;;;,,,,,;;;;;jGGjt;;itjjtit,:.
LEEGGLjjji;;;;,,,,;;iitttt;;tti;;tfGW#WW#WKWWWK#WW##W###WWWWW#WWKELtii;;;;,,,,;;;,;fji,;iii;iti,;,:.
fWDLGLfLjii;;;,,,;;;;;;;iii;iji;itfEWWWWW#WW#W##WW#W######W#WWWKGLjiii;;;;i;;;;ti;;ti;:,;i;,;;;:,,:.
fWEDGLffjjti;;,:,,,,,,;iii;;tjt;itfKWWWWWW##WW#WW###W##W###W#WELjtii;;;,;;ii;,,i,,ij;,,,;i;;;,,::...
fWWKGtttii;,;i,,;i;,;iit;tf;;tfiitjWWW##W#WW#WW##WWW########WKftitii;;;;t;tt;,,,::;,:,;;ii;;;i;:....
fWKKLtiii;,,;ti;tjtitii;,,;;tjftiiEWWWWWWW##WWW##W##K##W###WWEtii;;;;ffDKjfji;,,:;;;;;;itttiijt,:...
LWKEjttiii;ijjfGLjttti;;;;;iifLt;;EWW#W#WWWKWK#WW###WW#####WKGiiiiitjfffGjjti;;;;t;,;;ttijiiijti;:..
LKKEtittjLjjGDDEEGji;;;;;iittGGf;iKWWWWWWEEWW#WWWW#KKKW###WWKfii;itLGLjiffj;;;,;;ii;,,jjjiijt,,;,,:.
LKKDjijLGLfLDKKftjt,,::,;;;LffLGjKKWWWWWWKWWWW#WWW#WWKWW##WEGjii;;iLDKEtjjt;t;;,,;iittLLDjtLfi,,:::.
LKEELffLGLLDDEj,,;tt;::,ittfGfjDGDWWKWW#KWKW##WW###WWEEKWWKDft;i;;;tGK#Eitiii;,:,;LfjfLtLLtLjjti,:..
GKEKDGLGGGDEEDii;;itti;;jtijLfjtGKWWWWWKWEWW#####W#WWGDEKKEDfi;;;;,,;fGt;i;;i;,,ittjjtttjGGELLti;:..
GEEDGfGGLffLLiiji;;;;tjtfjjffLfjGWWWWWWWWWWW#######GKDDDEEEDtti;;;,,,tt;,i,;jfttij;jfitfjLGEEf;;,:..
GEEDLjLDDjtfi,ijt;iitff;tDDLfGLEGWW#WWWWW#######W##EGEDGDEEGjiii;i;;,,,,,,,;jGt;ttfjjjifDDfLEE;::,::
LEEDLjffjfGGiifGDjfttLDitffjfGjDDKWWWWKWW##W#W#####KGLGGDGDLttiiiiii;,,;;:;tit,,tiii;iiLGGGtLEi:.:,:
GEEELjfttfGLfjfGDLGGGKKfLfLGLLjGKKWWWWWW###########DtfGGLEGfjttttttti;,;;:ii;,,;;i,i;iiLLj;;fft:.::.
GEEGGLLLfjjfGELLLfGGGLEt;;tjGGLEW##WWWWW##W########WWEGLLLftttttttjti;,t;,.jt;iiiiijfitjjt;,;tt;,:,,
DDLGGDEDLjjjGEftjLEfffi,,,;tjjEWWWWWWW##WW#########WWWELLjjttittttttti;i;ttjf;ititffjitjfj;::;itiitj
DGLGEEKDjtjLGjttjfLLGj,,;i;iiiDWWWKKWW##W##########WWWELfjttiittttjtttGjjKji,,fGjjfLj;tLft;::,,ittjL
GDGDEKKLttfKDjtLDfLDDi,itttjiiEKWWWWW##############WWKELfjtttitttttjjttEftit;;DDLjfLjijjtti,::,;jjLG
GEDDEKEGjGWWKEKWWEEEEfjfLjttjLKKWKWWW##############WKKEGfjtttttttjjjjt;jfj;tjLEELjfffttiti;;::,,;fDE
GEDDEKKGjLKWW#WWKDGEEEDGDLjtjDKWWKWW###############WKKEDLjjttttjjjjffjt;LjtjfGDGGfLtLjffLjit;:,,,jEE
LEDDEEKGjjLKW###EttDEEGfjLDLEEKWWW##W###W##########WEEEGLfjjjjjtjjfffDDLjjftLLLftfjiLDEDffftj;i;,iDK
LEDEEEEDjtfEW#WWLtjEEGfjjfEKEEWWWWW################WWEEGGLjjjjjjjffLKDEGttjjfjttijfDGLLfjft,;ttt;;fD
LEDEKEEDffGWW#WWDfGELLffLfKEEWWWWW#################KDEDDLLfjjjjjjffLfLLftttjtjfGjifEELfjjj,.:,;ittjj
GEEKKWKEGDKWWW#WWEEELGfjLEEEEWWWW#################WKDDDDGLLfjjjjfLLfjLLjjjjfjjLEDjEWELfjjj;::,,ijfjj
DEEKWWWKEEWWWWWWWWWWEGjfGEEKKWWWW#################WELGEDGGLffjjffLLffjjtjLDLLLfEEjKWELGjfjti;itffffj
EEEKWW#WKKWWWWWWWWW#KLLEDDEKWWWWW##############W#WEELGDDGGLffjfffLfffttttfDGGGLEKDWWKEDitfLLLGGGGLLL
DDEKWW#WKKWWWWWWWWWWEGEKEEKEKWWW#################WGGLGGDGGLfjfffffffji;tjfLGDEEEWKDEGLGijGEEEDGLLLff
GDEKWW#WWKKKKKKKWWWWDEKKEEDLWWW########W########WEDLLLGDGGLGEGtjfffLfi;;;tGDDKEDKLtfDLftttfGfjiii;;;
GDKKWW#WWKKKWKDEKKWWKKKKKKDEWWW##W##############KELLLLGDGGGELjLEjjjfj:,tijDDDEEDEGjjDGjt;,iti;,,,,,,
EKKKWWWWWKKWWKDDKKWWWKKKWKKKWWW##WW###WW###WWW#WEGLffLLGGLEDLLDEL;tGi:,jjGGGDEDLLDGffGji;,;;;,,:,:::
WKKKWWWWWWWWWKDLGEKKWKEKWKKKWW##WWW##WWW##WWW#WEDLfffLLLLEDDEEKKGjfDLjLjiLLLDEDjfGGffGti;;;;;,,,,,,:
WKEEEEEKWWWKEEEGLDEKKEDGEKKWW###WW###WW##WWWWWDGLffffLLLDELDEEKWKEGGGLLLiijttDDLGDDGGLjttiiiiiiii;;;
WKEEt;;LWWWEDDDDDDEEGGGjLEKWW##W####WW#WWKWWWDfjjjjffLLLEEGDKEWWWEDDGGfLfit;,;GGKKEGGfGGGLfjffLLLfjj
WWKi:.:iKKKEEDEEDDDDGGfitKWWW##K###WWW#KEKWWfiiittjjfLLfGEDKKEKWKEDDDGtifftji;jDKKEDGfEKEEEEEEEEKEDD
WKt:  :jEEEEEEEEDDEDDDjtGKKWW##W###WW#GfKWL;,,,;ittjfLfEEDDEKEEKWWELGDLjffjGEfLDWWEDLLEEEEEEKKEEEKKE
Kt:. :iGDDDDDDDDDDDDGGLLKWEW##WW#WW##GDKDi....:,;ittjfKWWKKKKEEKWKKEEGGfjjLDEEGGWKDDfLDGLDKKEEGGDEEE
t:. .;LGGDGGGGGGGGGGLLGGGWWW#WW#WWWWGEj;.      .,;iitftGLKWKKKKDEKEDKKDGftLDDGLGEDLfjDEDDEWWWKDDEKKK
:. .,tLGDDGLLLLLLGGGLLGGDWW##W###WWKK:,        ...;;it;tGEKKWKKEEKEEKKDDt;tGEGfLDDLjfEWWWWW#W#WWWWWW
. .:j;iLGGLffLLLLLLGLLDGDKW######WWW.:           ...;i;;jEKKKKWWKKWWKGLDGtffGGfjGDfjGEKWWWWWWWWWWWWW
..:tGiifLLLffffLLLLLfLGLGKW#####WWW.:             ....;;;DKKKKKWKKKWKLfDKDLjfGjjGGtjLGEWKKKKKKKEDEEK
:,ifLjjLLffffffLLLLLfLLLGKW#W###WW:,               ...::tGKKKEKWKKKKKDfGEEGfLLjfGftjGDEEDEEEDEDDGDED
iitLLtjfffffffffLLLLLLGGDWWWW##WWG..               .....::KDGEKWKKWKKELLEKEjtjjffttLGGEEDDDDDDDDDDDD
Dj;tjtjffffLffjjfLLLLGGGG#######W::                 ....::EEEKKWEDEKKKDLDEDfttffLtfLLLGGGGGGGGGGGGGG
DL,;ttffjjfLffjjLLLLLGDGDW######E:                   ....:.WWKKKEDEKKKKGGDLLjtjfDLfjffLLLLLLLffffffL
DDjitjffjfjjffffLfLLLLDGGWW#####: ..                  ..:..EWKKEKKKWKKKEEDftttjfGfttjjffffffjfjfjjjj
DDjttjjfffjjfffffjfLLLDDDW#####W: ...   . .i,iit;;ft   .....KKKEKKWKWEEKKDfjLLfjfjtjjjjjjjjjjjjjjjjt
DGjtjjffffLfLfffffLLLLGDEW#####t....   .. GG.ttjLDf:,f .....;KKWWWWWWKDEDDLLDGftjjjjjjjjtttttttttttt
DLjjjfffffffffffLLGLLLGDE######: .: .  .. ,.       .jE,:.....EKWWWWWWWKELGDDEGjjjjttttttttttttititti
DfjjfLLfffffLLLLLGDGGEEEKWW###L .:... ...        .    .j......KKWWWWWKKKGDDDEGjtjtttttttiiiiiiiiiiii
DjjfLLfLLfjfGLGGGDDDDDEE#W#W##,.::....... .:...::::..   ;.....iKWWWWWKKEDDGLffttjtttiiii;;;;;;;;;;;,
LjffLLLGLfffGGDDDEDDEEKKWW###W..,.........    :,:::...:: :.....KWWKKKKKEfGDGfffjjtttii;;;;;;;,,,,,,,
LLLLGGGDDGGDEEEEEEDDKKKWWW###,.:,.....  .:  :,,:..:...:,, :.... KWKKWKWGjLDDGLLLttti;;;;,,;;;,,,,,,,
LGLLGGLGDDDEEEEKKKKKKKKW#####:.::........:..,,:::::...:,,:......tKKWWKKDfDKGjjfLttti;;;;,;;;;,,,,,,,
GGfLLLLGDEEEEEEKKKKKKKKWW####..::........::;;,::::::...:,,,......KWWWWEDLDEGjijftiiiii;i;;;i;;;;;;;;
LGffffGEKKKKWKKWWWKWKKKW####j.::::.......:;,,::::,:....:,,:;:.....WWWKKEGEEKGtfjiiiiiiiiiiiiiiiii;;;
LLfLfLEWWWWWWWW#WWWKKEWWW###:.::::::...:::,:,::,:::.....:,,;:.....EWWKKKDEKWKLLtiiiiiiiiiiii;ii;;;;;
LffGGDKWWWWWWWWWKKEEEEWWWW##..::.:::.:.:::,,:,,:::::....:,,:,:.....WWWEEEEKWWKDt;;iiii;;;;;,,;;,,,,,
LffDDDDEEDDEDDEDDEDDDDEKWWWW..::::::::.:..:,:,:,:::.....:,,,;,.....tWKEEEEKWWWDt;;iiii;;,,,:,,,:::::
LLfGGLLGGLGGDDDDDGGDGEEWWE#W.::::::::::::.,,:,,,:::......:,,;,:.....EEEKEDEWWWDi;;;iii;;,,,:,;;,::::
LfjGLLLGGLLGDDGDDLGDGEEWEEWW..:::::,,..:..,,,,,,::.......:,,:,,.....jEEKGGEWWWGt;;iiiii;;,,:,,,,::::
ffjLGGGGGLGDGLLDGLGDEEKWGE#W...::,:,:.::.::,,,,,::.....::::,,,,,.....KKEDDKWKKDj;;iiiiii;,,,,,,,::::
ffjfLLGGLLGDGLLDGLLGEKWWWKWW...::,,,:.:.::,,,:,:,:......::,:,;;,... .EEDDEDKKWEj;;iiiiiii;,,,,,,::::
LftjLLGLLGGDGLGGKGfjDWWWEEW#....:,,,::::::,,,,::::..  ..:::,,,,,:....DEDEEGGDEDfi;;iiiiii;,,:,,:::::
LfjjLGDGLGGGGGGGGEEEEWDGE#W#....::,:::.:::,,,::::.. .....:::,:,,,... GKDEEEDEKEELiiiiiiii;,:,,,:::::
GLjjLDDGGLGGLLLGLLKKEGKWWWW#i...::,:.:::::,,,::....:.::...:.,:,,,:....KDLLDEDEDDLjttttttti,,,;;,::::
GGffDDGGGGDLLLGLfLGKWWW##WW#i;...:,:::,,::,,:::,,,;,;t;: ,i::::,,,:...EDjtGDGLjffjfjjjjttt;;;i;,,:,:
DGffGDLLLGDLLLGfffLGGLW#####,t:.::,:::,,::::;:,;ii;;iitt:::i::;:,,:...LEfjGLLftjLfffjjjttttiti;,,,,:
KEGLGGLffGGLfffjjjfGfffL####;i,:::::::,:::tiiii;i,,;iiii:,;:;,f:,,,:...WKKKKKKDDEEGEfjtttttttt;,,,,,
KEEEDGffLGLjtfffffLGjtjW####;;,.:::..:::jt;;;ii;;i;;iii;:,;;,,,;:::::..WWWWWWKKEEEDEftiiiiitti;,,,,,
DDDDDGLLGGLfjLGfffLGtitjjGW#f;,::::.::,ii;i;;;i;;iiii;;,:,:,;;;W:,:::::WWWWWWWWKGEKEt;;;;;;;;;;,,,,,
GDDDEDDGGLLfLGLjtjLLtittjjjW#;;::.:.:;i;;;,;;;i;;iii;;i.,,,::,:Gj::::::WWWWWWWWWDGGDi;;;;,,,,,,,,,,,
DDEEDGGGDGGGGDLLffLftttjjttjfi;::.::;t,i;,,;,;;;;;;;;;;.,,,,;;:ED:,:,,:KKWWWWWWWWEEKt;;;;,;,,,,,,,,,
DGDEDGLGEDGGGLffffjitttjjiitji;,::::j;,i,;;;;;;;;;;;;;,.:,,:,;,.L#:,,,,WWWWWKWWWWWKEt;;;;;;;,,,,,,,,
LGDEEGLGDGLLLLjjjftitttjftitjLit,,:;j,;i,,,;;,;;,,;;;,::,;,::;,.EG,,:;,WWWWWKWWKKWEEt;;;;;;;;;;;;;,,
GGEEDGGGLfffGLfjtjffjjttjtttjfiii,,tj:ii,:,;;;;;,,,,,,.:,;;,.,,,.GW:,;,WWKWWWWWWWWKGjttttiiitttiiii;
GLLDDDDLffjfGLffjjLfjtitfjjjjLtij;,tf,t;::,,,,,,,,,,,,.,,,,,:,,::;f;;i,KWKKWWWWWWKKLijjjjjjjtttttttt
GLfGGGDLffjfLfjjjfGjtiitfjtjjLLitt:tf;i;,,,;,,,,,,,,,::,,,,,:;;:;.KG:i;WKKWWKWWWWWWGtjjjjjjttttttttt
EEGLLGDGfLfLLffjjLLtiiitfjjjjLfjij,jj;ti,,,,,,,,,,,,,.:,,,,::;;:;::L:iiWWWKWWWWWWWWKLjjjtttttttttiii
DDLffLGGLLfLLjjjjfftiiitftttjGfftttjj;i;:,,,,,,,:::,,.:,,,,,:,,:i,,E;iLWWKKWKKWWWWWKDtttttttttiiiiii
DLfjfLDLffffjttjjfftiiiijtjjfLLLjttfj;it:,,,,:,,,::,,.::,,,,:,,,ii,WtiKWWKKWKKWWWWWEGtiiiiiiiiiiiiii
DfjjjGGfjfLLjjjjfLLjjjjffffffLfjfjtffiti,,,,,,,:,:::::,:,,,,:,,:ii,LtiKWWWKKWWWWWKKKGtiiiiiiiiiiii;i
DGLLLDGfffLGLfffLLfjtttjfjjjffLjfjjtLtti:::,:,:,::::.:,,,,,,,:,:ii;f;;KKWWKKWKWWWWKKEjii;i;;;;;;;;;;
GLLLGGfjfjfLfjjjLGLftttjfjjtjfffLLjjjGtt:,,,,,,,,::: ::,,,,,::,,it;Ei;KWWWKKWKWWWWWWELi;;;;;;;;;;;;;
GGGLGLfjjjfffjjjLDGLjtttjjjjjffjfLffjLti,,,,:,,,,:::.::,,,,:,:,;tt;LiiKKKWWWWWWWWWWWKDi;;;;;;;;;;,,,
GGLLDLffjfffffffGGDGfjtjfjjjjLLLffLjfjtt,,,,,:,:,:::.:::::,:::,itt;ti;KKKWWWWWWWWWKKKEi;;;,,,,,,,,,,
LLLGGjjjffLffjjfLGGLfjjjfjjttjjffjjLfjjt,,,,,,,,,::::::::::,,:,ittDtijWKKWWWWWWWWWWKEDi;;;;;,,,,,,,,
fjLDjijfLLGLfjfLGLfjjjjjfjjjjfLGGLLDjftt;,,,,,:,,,,.:::,,,,,,:,ttt#tiWWWKWWWWWWWWWWWKEj;;;i;;;,,,,,,
jjLEfjjfjfLfjttLGLjtjtttjjtjfLLLGGGDEjftt,,,,:::::: ::::::::::,ttKKjiWWWKKWWWWWWWKWWKKDi;ii;;;;;,,,,
jjfGfffjtjjjjffLGGjtjjiifjjjfLLLGGGGELjLt,,,,,,::::.::::::,:,,,ttWELKEWWWKKKWWWWWWWWWWKj;ii;;;,,,,;;
jjjffffjjffjjffLLLftttitjjjtjfffLLGLGLjft,,,,,:::,,.::::::,,,:;tjWKE,WKWWWKKKWWWWWWWWWWjii;;;,,,,,,;
**/

// Tribute to a friend I owe countless. Thanks, TN.


#include<bits/stdc++.h>
// #define int long long int
#define pll pair<int,int>
#define dbl long double
#define ff first
#define ss second
#define endl "\n"
#define PI acos(-1)
#define mod 1000000007
#define eps 0.00000001
#define INF 1000000000000000001
#define all(x) (x).begin(),(x).end()
#define LB(v,x) (lower_bound(all(v),x) - v.begin())
#define UB(v,x) (upper_bound(all(v),x) - v.begin())
#define size(x) (int)(x).size()
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define popb() pop_back()
#define popf() pop_front()
#define mp(x,y) make_pair((x),(y))
#define vec(dt) vector<dt>
#define vv(dt) vector<vector<dt>>
#define count_set_bits(x) __builtin_popcount(x)
#define fastio(x) ios_base::sync_with_stdio(x); cin.tie(NULL)
#define init(v,s) memset(v,s,sizeof(v))
#define bug(x) cerr<<"LINE: "<<__LINE__<<" || click to see test details "<<#x<<" = "<<x<<endl
#define loop(i,s,n) for(int i=s;i<n;i++)
#define print(v) for(auto it:v) cout<<it<<" "; cout<<endl
using namespace std;

signed main()
{
	fastio(0);
	int n;
	cin>>n;
	int a[n+1],dp[n+1][n+1],mx[n+1][n+1];
	loop(i,1,n+1)
	{
		cin>>a[i];
		mx[i][i] = dp[i][i] = a[i];
	}
	loop(window,2,n+1)
	{
		loop(l,1,n+1)
		{
			int r = l + window - 1;
			if(r>n) break;
			dp[l][r] = dp[l+1][r] ^ dp[l][r-1];
			mx[l][r] = max(dp[l][r], max(mx[l+1][r], mx[l][r-1]));
		}
	}
	int q;
	cin>>q;
	loop(i,0,q)
	{
		int l,r;
		cin>>l>>r;
		cout<<mx[l][r]<<endl;
	}
 	return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define pll pair<int,int>
#define dbl long double
#define ff first
#define ss second
#define endl "\n"
#define PI acos(-1)
#define mod 1000000007
#define eps 0.00000001
#define INF 1000000000000000001
#define all(x) (x).begin(),(x).end()
#define LB(v,x) (lower_bound(all(v),x) - v.begin())
#define UB(v,x) (upper_bound(all(v),x) - v.begin())
#define size(x) (int)(x).size()
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define popb() pop_back()
#define popf() pop_front()
#define mp(x,y) make_pair((x),(y))
#define vec(dt) vector<dt>
#define vv(dt) vector<vector<dt>>
#define count_set_bits(x) __builtin_popcount(x)
#define fastio(x) ios_base::sync_with_stdio(x); cin.tie(NULL)
#define init(v,s) memset(v,s,sizeof(v))
#define bug(x) cerr<<"LINE: "<<__LINE__<<" || click to see test details "<<#x<<" = "<<x<<endl
#define loop(i,s,n) for(int i=s;i<n;i++)
#define print(v) for(auto it:v) cout<<it<<" "; cout<<endl
using namespace std;

signed main()
{
	fastio(0);
	int n;
	cin>>n;
	int a[n+1],dp[n+1][n+1],mx[n+1][n+1];
	loop(i,1,n+1)
	{
		cin>>a[i];
		mx[i][i] = dp[i][i] = a[i];
	}
	loop(window,2,n+1)
	{
		loop(l,1,n+1)
		{
			int r = l + window - 1;
			if(r>n) break;
			dp[l][r] = dp[l+1][r] ^ dp[l][r-1];
			mx[l][r] = max(dp[l][r], max(mx[l+1][r], mx[l][r-1]));
		}
	}
	int q;
	cin>>q;
	loop(i,0,q)
	{
		int l,r;
		cin>>l>>r;
		cout<<mx[l][r]<<endl;
	}
 	return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define pll pair<int,int>
#define dbl long double
#define ff first
#define ss second
#define endl "\n"
#define PI acos(-1)
#define mod 1000000007
#define eps 0.00000001
#define INF 1000000000000000001
#define all(x) (x).begin(),(x).end()
#define LB(v,x) (lower_bound(all(v),x) - v.begin())
#define UB(v,x) (upper_bound(all(v),x) - v.begin())
#define size(x) (int)(x).size()
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define popb() pop_back()
#define popf() pop_front()
#define mp(x,y) make_pair((x),(y))
#define vec(dt) vector<dt>
#define vv(dt) vector<vector<dt>>
#define count_set_bits(x) __builtin_popcount(x)
#define fastio(x) ios_base::sync_with_stdio(x); cin.tie(NULL)
#define init(v,s) memset(v,s,sizeof(v))
#define bug(x) cerr<<"LINE: "<<__LINE__<<" || click to see test details "<<#x<<" = "<<x<<endl
#define loop(i,s,n) for(int i=s;i<n;i++)
#define print(v) for(auto it:v) cout<<it<<" "; cout<<endl
using namespace std;

signed main()
{
	fastio(0);
	int n;
	cin>>n;
	int a[n+1],dp[n+1][n+1],mx[n+1][n+1];
	loop(i,1,n+1)
	{
		cin>>a[i];
		mx[i][i] = dp[i][i] = a[i];
	}
	loop(window,2,n+1)
	{
		loop(l,1,n+1)
		{
			int r = l + window - 1;
			dp[l][r] = dp[l+1][r] ^ dp[l][r-1];
			mx[l][r] = max(dp[l][r], max(mx[l+1][r], mx[l][r-1]));
		}
	}
	int q;
	cin>>q;
	loop(i,0,q)
	{
		int l,r;
		cin>>l>>r;
		cout<<mx[l][r]<<endl;
	}
 	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long

int main(){

    ll n;
    cin >> n;
    ll a[n+5] = {0};
    for(ll i = 1;i<=n;i++){
        cin >> a[i];
    }

    ll dp[n+5][n+5];

    memset(dp,0,sizeof(dp));

    for(ll i = 1;i<=n;i++){
        for(ll j = 1;(j+i-1)<=n;j++){
            ll st = j,en = j+i-1;
            if(st<en)
            dp[st][en] = dp[st+1][en]^dp[st][en-1];
            else if(st == en){
                dp[st][en] = a[st];
            }
        }
    }

    // for(ll i = 1;i<=n;i++){
    //     for(ll j = 1;j<=n;j++){
    //         cout << dp[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    ll mx[n+5][n+5];
    memset(mx,0,sizeof(mx));

    for(ll i = 1;i<=n;i++){
        for(ll j = 1;(j+i-1)<=n;j++){
            ll st = i,en = i+j-1;
            if(st<en){
                mx[st][en] = max({dp[st][en],mx[st][en-1],mx[st+1][en]});
            }
            else if(st == en)mx[st][en] = dp[st][en];
        }
    }

    ll q;
    cin >> q;
    while(q--){
        ll l,r;
        cin >>  l >> r;

        cout << mx[l][r] << endl;

    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long

int main(){

    ll n;
    cin >> n;
    ll a[n+5] = {0};
    for(ll i = 1;i<=n;i++){
        cin >> a[i];
    }

    ll dp[n+5][n+5];

    memset(dp,0,sizeof(dp));

    for(ll i = 1;i<=n;i++){
        for(ll j = 1;(j+i-1)<=n;j++){
            ll st = j,en = j+i-1;
            if(st<en)
            dp[st][en] = dp[st+1][en]^dp[st][en-1];
            else if(st == en){
                dp[st][en] = a[st];
            }
        }
    }

    ll mx[n+5][n+5];
    memset(mx,0,sizeof(mx));

    for(ll i = 1;i<=n;i++){
        for(ll j = 1;(j+i-1)<=n;j++){
            ll st = j,en = i+j-1;
            if(st<en){
                mx[st][en] = dp[st][en];
                mx[st][en] = max(mx[st][en-1],max(mx[st+1][en],mx[st][en]));
            }
            else if(st == en)mx[st][en] = dp[st][en];
        }
    }

    ll q;
    cin >> q;
    while(q--){
        ll l,r;
        cin >>  l >> r;

        cout << mx[l][r] << endl;

    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long

int main(){

    ll n;
    cin >> n;
    ll a[n+5] = {0};
    for(ll i = 1;i<=n;i++){
        cin >> a[i];
    }

    ll dp[n+5][n+5];

    memset(dp,0,sizeof(dp));

    for(ll i = 1;i<=n;i++){
        for(ll j = 1;(j+i-1)<=n;j++){
            ll st = j,en = j+i-1;
            if(st<en)
            dp[st][en] = dp[st+1][en]^dp[st][en-1];
            else if(st == en){
                dp[st][en] = a[st];
            }
        }
    }
    // ll mx[n+5][n+5];
    // memset(mx,0,sizeof(mx));

    for(ll i = 1;i<=n;i++){
        for(ll j = 1;(j+i-1)<=n;j++){
            ll st = j,en = i+j-1;
            if(st<en){
                // dp[st][en] = dp[st][en];
                dp[st][en] = max(dp[st][en-1],max(dp[st+1][en],dp[st][en]));
            }
        }
    }

    ll q;
    cin >> q;
    while(q--){
        ll l,r;
        cin >>  l >> r;

        cout << dp[l][r] << endl;

    }

    return 0;
}


#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 5111;

int n, a[MAXN], dp[MAXN][MAXN], ans[MAXN][MAXN];

int32_t main() {
#ifdef Fekete
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    //freopen("debuger.txt", "w", stderr);
#else
    //freopen("chess2.in", "r", stdin);
    //freopen("chess2.out", "w", stdout);

#endif // Fekete
    ios::sync_with_stdio(0);cin.tie(0);

    scanf("%d", &n);
       for(int i = 1; i <= n; i++) {
       	scanf("%d", &a[i]);
       	dp[i][i] = ans[i][i] = a[i];

       }
       for(int len = 2; len <= n; len++) {
       	for(int i = 1; i + len - 1 <= n; i++) {
       		int j = i + len - 1;
       		ans[i][j] = ans[i][j - 1] ^ ans[i + 1][j];
       		dp[i][j] = max({dp[i + 1][j], dp[i][j - 1], ans[i][j]});
       	}
       }
       int q;
       scanf("%d", &q);
       while(q--) {
       	int l, r;
       	scanf("%d%d", &l, &r);
       	cout << dp[l][r] << "\n";
       }

}


#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 5111;

int n, a[MAXN], dp[MAXN][MAXN], ans[MAXN][MAXN];

int32_t main() {
#ifdef Fekete
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    //freopen("debuger.txt", "w", stderr);
#else
    //freopen("chess2.in", "r", stdin);
    //freopen("chess2.out", "w", stdout);

#endif // Fekete
    ios::sync_with_stdio(0);cin.tie(0);

    scanf("%d", &n);
       for(int i = 1; i <= n; i++) {
       	scanf("%d", &a[i]);
       	ans[i][i] = a[i];
       }
       for(int len = 2; len <= n; len++) {
       	for(int i = 1; i + len - 1 <= n; i++) {
       		int j = i + len - 1;
       		ans[i][j] = ans[i][j - 1] ^ ans[i + 1][j];
       		dp[i][j] = max({dp[i + 1][j], dp[i][j - 1], dp[i + 1][j - 1], ans[i][j]});
       	}
       }
       int q;
       scanf("%d", &q);
       while(q--) {
       	int l, r;
       	scanf("%d%d", &l, &r);
       	cout << dp[l][r] << "\n";
       }

}


#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 5111;

int n, a[MAXN], dp[MAXN][MAXN], ans[MAXN][MAXN];

int32_t main() {
#ifdef Fekete
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    //freopen("debuger.txt", "w", stderr);
#else
    //freopen("chess2.in", "r", stdin);
    //freopen("chess2.out", "w", stdout);

#endif // Fekete
    ios::sync_with_stdio(0);cin.tie(0);

    scanf("%d", &n);
       for(int i = 1; i <= n; i++) {
       	scanf("%d", &a[i]);
       	ans[i][i] = a[i];
       }
       for(int len = 2; len <= n; len++) {
       	for(int i = 1; i + len - 1 <= n; i++) {
       		int j = i + len - 1;
       		ans[i][j] = ans[i][j - 1] ^ ans[i + 1][j];
       		dp[i][j] = max({dp[i + 1][j], dp[i][j - 1], ans[i][j]});
       	}
       }
       int q;
       scanf("%d", &q);
       while(q--) {
       	int l, r;
       	scanf("%d%d", &l, &r);
       	cout << dp[l][r] << "\n";
       }

}


#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define LL long long
#define PLL pair<LL, LL>
#define LD long double
#define VI vector<int>
#define VPII vector<PII>
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define pb pop_back
#define FOR(a,s,d) for (int a = (s); a <= (int)(d); ++a)
#define FORD(a,s,d) for (int a = (s); a >= (int)(d); --a)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RE(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ALL(x) x.begin(), x.end()
#define siz(x) ((int)x.size())
#define Sim template <class s
Sim, class d> ostream & operator<<(ostream &os, pair<s,d> x){return os << "<" << x.F << ", " <<
  x.S << "> ";}
Sim> auto operator<<(ostream &os, s x) -> typename enable_if<!is_same<s,string>::value,
  decltype(x.begin(), os)>::type
{ int o = 0; os << "{"; for (auto c : x) {if(o++) os << ", "; os << c;}return os << "} ";}
void dor() {cerr << endl;}
Sim, class ...t> void dor(s x, t...y) {cerr << x << " "; dor(y...);}
#ifdef DEB
#define debug(c...) dor(__FUNCTION__, ":", __LINE__, ": ", c)
#else
#define debug(...)
#endif
const int M = 5005;

int T[M][M], dp[M][M], n;

int main()
{
  //ios_base::sync_with_stdio(0);
  //cin.tie(0);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
  {
    scanf("%d", T[1] + i);
    dp[0][i] = T[0][i];
  }
  for (int i = 2; i <= n; i++)
  {
    for (int j = 1; j + i <= n + 1; j++)
    {
      T[i][j] = T[i - 1][j] ^ T[i - 1][j + 1];
      dp[i][j] = max(T[i][j], T[i - 1][j]);
      dp[i][j] = max(dp[i][j], dp[i - 1][j + 1]);
    }
  }
  int q;
  scanf("%d", &q);
  REP(i, q)
  {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", dp[r - l + 1][l]);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define LL long long
#define PLL pair<LL, LL>
#define LD long double
#define VI vector<int>
#define VPII vector<PII>
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define pb pop_back
#define FOR(a,s,d) for (int a = (s); a <= (int)(d); ++a)
#define FORD(a,s,d) for (int a = (s); a >= (int)(d); --a)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RE(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ALL(x) x.begin(), x.end()
#define siz(x) ((int)x.size())
#define Sim template <class s
Sim, class d> ostream & operator<<(ostream &os, pair<s,d> x){return os << "<" << x.F << ", " <<
  x.S << "> ";}
Sim> auto operator<<(ostream &os, s x) -> typename enable_if<!is_same<s,string>::value,
  decltype(x.begin(), os)>::type
{ int o = 0; os << "{"; for (auto c : x) {if(o++) os << ", "; os << c;}return os << "} ";}
void dor() {cerr << endl;}
Sim, class ...t> void dor(s x, t...y) {cerr << x << " "; dor(y...);}
#ifdef DEB
#define debug(c...) dor(__FUNCTION__, ":", __LINE__, ": ", c)
#else
#define debug(...)
#endif
const int M = 5005;

int T[M][M], dp[M][M], n;

int main()
{
  //ios_base::sync_with_stdio(0);
  //cin.tie(0);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
  {
    scanf("%d", T[1] + i);
    dp[1][i] = T[1][i];
  }
  for (int i = 2; i <= n; i++)
  {
    for (int j = 1; j + i <= n + 1; j++)
    {
      T[i][j] = T[i - 1][j] ^ T[i - 1][j + 1];
      dp[i][j] = max(T[i][j], T[i - 1][j]);
      dp[i][j] = max(dp[i][j], dp[i - 1][j + 1]);
    }
  }
  int q;
  scanf("%d", &q);
  REP(i, q)
  {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", dp[r - l + 1][l]);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define LL long long
#define PLL pair<LL, LL>
#define LD long double
#define VI vector<int>
#define VPII vector<PII>
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define pb pop_back
#define FOR(a,s,d) for (int a = (s); a <= (int)(d); ++a)
#define FORD(a,s,d) for (int a = (s); a >= (int)(d); --a)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RE(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ALL(x) x.begin(), x.end()
#define siz(x) ((int)x.size())
#define Sim template <class s
Sim, class d> ostream & operator<<(ostream &os, pair<s,d> x){return os << "<" << x.F << ", " <<
  x.S << "> ";}
Sim> auto operator<<(ostream &os, s x) -> typename enable_if<!is_same<s,string>::value,
  decltype(x.begin(), os)>::type
{ int o = 0; os << "{"; for (auto c : x) {if(o++) os << ", "; os << c;}return os << "} ";}
void dor() {cerr << endl;}
Sim, class ...t> void dor(s x, t...y) {cerr << x << " "; dor(y...);}
#ifdef DEB
#define debug(c...) dor(__FUNCTION__, ":", __LINE__, ": ", c)
#else
#define debug(...)
#endif
const int M = 5005;

int T[M][M], dp[M][M], n;

int main()
{
  //ios_base::sync_with_stdio(0);
  //cin.tie(0);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
  {
    scanf("%d", T[1] + i);
    dp[1][i] = T[1][i];
  }
  for (int i = 2; i <= n; i++)
  {
    for (int j = 1; j + i <= n + 1; j++)
    {
      T[i][j] = T[i - 1][j] ^ T[i - 1][j + 1];
      dp[i][j] = max(T[i][j], dp[i - 1][j]);
      dp[i][j] = max(dp[i][j], dp[i - 1][j + 1]);
    }
  }
  int q;
  scanf("%d", &q);
  REP(i, q)
  {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", dp[r - l + 1][l]);
  }
  return 0;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define rep(i,a,b) for(i=(a);i<=(b);i++)
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int,int>
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define maxn 100005
#define inf 1e18
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define fr freopen("source.txt","r",stdin),freopen("output.txt","w",stdout)
#define SET(a,b) memset(a,b,sizeof(a))
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define all(x) x.begin(), x.end()
#define ps(x,y) fixed<<setprecision(y)<<x
//template<typename T>
//using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out <<  "(" << a.F << ", " << a.S << ")";
}

template<class A> ostream& operator << (ostream& out, const vector<A> &a) {
	out << "[";
	for (auto it = a.begin(); it != a.end(); ++it) {
		if (it != a.begin())
			out << ", ";
		out << *it;
	}
	return out << "]";
}
const int N = 5005;
ll v[N][N], dp[N][N];
ll go(ll i, ll j) {
	if (i == j) return v[i][j];
	ll &ans = dp[i][j];
	if (ans != -1) return ans;
	ans = v[i][j];
	ans = max(ans, go(i + 1, j));
	ans = max(ans, go(i, j - 1));
	return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	boost;
	ll n, i, j, x, t, y, q;
	cin >> n;
	ll a[n];
	for (i = 0; i < n; i++) cin >> a[i];
	for (i = 0; i < n; i++) {
		x = a[i];
		v[i][i] = x;
		for (j = i + 1; j < n; j++) {
			x ^= a[j];
			if ((j - i + 1) % 2 == 1) v[i][j] = a[i] ^ a[j];
			else v[i][j] = x;
		}
	}
	SET(dp, -1);
	go(0, n - 1);
	cin >> q;
	while (q--) {
		cin >> x >> y;
		x--;
		y--;
		cout << go(x, y) << "\n";
	}
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define rep(i,a,b) for(i=(a);i<=(b);i++)
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int,int>
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define maxn 100005
#define inf 1e18
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define fr freopen("source.txt","r",stdin),freopen("output.txt","w",stdout)
#define SET(a,b) memset(a,b,sizeof(a))
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define all(x) x.begin(), x.end()
#define ps(x,y) fixed<<setprecision(y)<<x
//template<typename T>
//using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out <<  "(" << a.F << ", " << a.S << ")";
}

template<class A> ostream& operator << (ostream& out, const vector<A> &a) {
	out << "[";
	for (auto it = a.begin(); it != a.end(); ++it) {
		if (it != a.begin())
			out << ", ";
		out << *it;
	}
	return out << "]";
}
const int N = 5005;
ll v[N][N], dp[N][N], a[N];
ll go1(ll i, ll j) {
	if (i == j) return a[i];
	ll &ans = v[i][j];
	if (ans != -1) return ans;
	ans = go1(i + 1, j)^go1(i, j - 1);
	return ans;
}
ll go(ll i, ll j) {
	if (i == j) return go1(i, j);
	ll &ans = dp[i][j];
	if (ans != -1) return ans;
	ans = go1(i, j);
	ans = max(ans, go(i + 1, j));
	ans = max(ans, go(i, j - 1));
	return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	boost;
	ll n, i, j, x, t, y, q;
	cin >> n;
	for (i = 0; i < n; i++) cin >> a[i];
	for (i = 0; i < n; i++) {
		x = a[i];
		v[i][i] = x;
		for (j = i + 1; j < n; j++) {
			x ^= a[j];
			if ((j - i + 1) % 2 == 1) v[i][j] = a[i] ^ a[j];
			else v[i][j] = x;
		}
	}
	SET(v, -1);
	SET(dp, -1);
	go1(0, n - 1);
	go(0, n - 1);
	cin >> q;
	while (q--) {
		cin >> x >> y;
		x--;
		y--;
		cout << go(x, y) << "\n";
	}
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define rep(i,a,b) for(i=(a);i<=(b);i++)
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int,int>
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define maxn 100005
#define inf 1e18
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define fr freopen("source.txt","r",stdin),freopen("output.txt","w",stdout)
#define SET(a,b) memset(a,b,sizeof(a))
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define all(x) x.begin(), x.end()
#define ps(x,y) fixed<<setprecision(y)<<x
//template<typename T>
//using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out <<  "(" << a.F << ", " << a.S << ")";
}

template<class A> ostream& operator << (ostream& out, const vector<A> &a) {
	out << "[";
	for (auto it = a.begin(); it != a.end(); ++it) {
		if (it != a.begin())
			out << ", ";
		out << *it;
	}
	return out << "]";
}
const int N = 5005;
ll v[N][N], dp[N][N];
ll go(ll i, ll j) {
	if (i == j) return v[i][j];
	ll &ans = dp[i][j];
	if (ans != -1) return ans;
	ans = v[i][j];
	ans = max(ans, go(i + 1, j));
	ans = max(ans, go(i, j - 1));
	return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	boost;
	ll n, i, j, x, t, y, q;
	cin >> n;
	ll a[n];
	for (i = 0; i < n; i++) cin >> a[i];
	for (i = 0; i < n; i++) {
		x = a[i];
		v[i][i] = x;
		for (j = i + 1; j < n; j++) {
			x ^= a[j];
			if ((j - i + 1) % 2 == 0) v[i][j] = a[i] ^ a[j];
			else v[i][j] = x;
		}
	}
	SET(dp, -1);
	go(0, n - 1);
	cin >> q;
	while (q--) {
		cin >> x >> y;
		x--;
		y--;
		cout << go(x, y) << "\n";
	}
}


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

const int maxn = 5500;

int a[maxn];
int ans[maxn][maxn];
int ma[maxn][maxn];

int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;i++){
		for(int l=0;l+i<n;l++){
			if(i==0){
				ans[l][0]=a[l];
				continue;
			}
			ans[l][i]=ans[l][i-1];//^(ans[l-1][i-1]);
			ans[l][i]^=ans[l+1][i-1];
			ma[l][i]=max(ans[l][i],max(ma[l+1][i-1],ma[l][i-1]));
			//printf("ans[%d][%d] = %d\n",l,l+i,ans[l][i]);
		}
	}

	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%d\n",ma[l-1][r-l]);
	}

	return 0;
}


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

const int maxn = 5500;

int a[maxn];
int ans[maxn][maxn];
int ma[maxn][maxn];

int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;i++){
		for(int l=0;l+i<=n;l++){
			if(i==0){
				ans[l][0]=a[l];
				continue;
			}
			ans[l][i]=ans[l][i-1];//^(ans[l-1][i-1]);
			ans[l][i]^=ans[l+1][i-1];
			if(i>=1)ma[l][i]=max(ans[l][i],max(ma[l+1][i-1],ma[l][i-1]));
			else ma[l][i]=a[l];
			//printf("ans[%d][%d] = %d\n",l,l+i,ans[l][i]);
		}
	}

	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%d\n",ma[l-1][r-l]);
	}

	return 0;
}


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

const int maxn = 5500;

int a[maxn];
int ans[maxn][maxn];
int ma[maxn][maxn];

int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;i++){
		for(int l=0;l+i<=n;l++){
			if(i==0){
				ans[l][0]=a[l];
				continue;
			}
			ans[l][i]=ans[l][i-1];//^(ans[l-1][i-1]);
			ans[l][i]^=ans[l+1][i-1];
			if(i>=1)ma[l][i]=max(ans[l][i],max(ma[l+1][i-1],ma[l][i-1]));
			else ma[l][i]=a[l][i];
			//printf("ans[%d][%d] = %d\n",l,l+i,ans[l][i]);
		}
	}

	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%d\n",ma[l-1][r-l]);
	}

	return 0;
}


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

const int maxn = 5500;

long long a[maxn];
long long ans[maxn][maxn];
long long ma[maxn][maxn];

int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
	}
	for(int i=0;i<n;i++){
		for(int l=0;l+i<=n;l++){
			if(i==0){
				ans[l][0]=a[l];
				ma[l][0]=a[l];
				continue;
			}
			ans[l][i]=ans[l][i-1];//^(ans[l-1][i-1]);
			ans[l][i]^=ans[l+1][i-1];
			ma[l][i]=max(ans[l][i],max(ma[l+1][i-1],ma[l][i-1]));
			//printf("ans[%d][%d] = %d\n",l,l+i,ans[l][i]);
		}
	}

	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%lld\n",ma[l-1][r-l]);
	}

	return 0;
}


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

const int maxn = 5500;

long long a[maxn];
long long ans[maxn][maxn];
long long ma[maxn][maxn];

int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
	}
	for(int i=0;i<n;i++){
		for(int l=0;l+i<=n;l++){
			if(i==0){
				ans[l][0]=a[l];
				continue;
			}
			ans[l][i]=ans[l][i-1];//^(ans[l-1][i-1]);
			ans[l][i]^=ans[l+1][i-1];
			if(i>=1)ma[l][i]=max(ans[l][i],max(ma[l+1][i-1],ma[l][i-1]));
			else ma[l][i]=a[l];
			//printf("ans[%d][%d] = %d\n",l,l+i,ans[l][i]);
		}
	}

	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%lld\n",ma[l-1][r-l]);
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int long long
const int inf = 998244353;

int v[5001][5001];
int dp[5001][5001];

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
/*
    freopen("numbers.in", "r", stdin);
    freopen("numbers.out", "w", stdout);
*/

    int kol_vo;
    cin >> kol_vo;
    for(int c=0; c<kol_vo; c++)
        cin >> v[0][c];
    for(int c=1; c<kol_vo; c++){
        for(int i=0; i<kol_vo-c; i++){
            v[c][i] = v[c-1][i] ^ v[c-1][i+1];
        }
    }/*
    cout << endl << endl;
    for(int c=0; c<kol_vo; c++){
        for(int i=0; i<kol_vo; i++)
            cout << v[c][i] << " ";
        cout << endl;
    }*/
    //cout << endl << endl;
    for(int l=kol_vo-1; l>=0; l--){
        for(int r=l; r<kol_vo; r++){
            dp[l][r] = max(v[r-l][l], (r-l>0?max(dp[l][r-1], dp[l+1][r]):0));
        }
    }/*
    cout << endl << endl;
    for(int c=0; c<kol_vo; c++){
        for(int i=0; i<kol_vo; i++)
            cout << dp[c][i] << " ";
        cout << endl;
    }
    cout << endl << endl;*/
    int ask;
    cin >> ask;
    for(int c=0; c<ask; c++){
        int l, r;
        cin >> l >> r;
        l--; r--;
        cout << dp[l][r] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int long long
const int inf = 998244353;

int v[5001][5001];
int dp[5001][5001];

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
/*
    freopen("numbers.in", "r", stdin);
    freopen("numbers.out", "w", stdout);
*/

    int kol_vo;
    cin >> kol_vo;
    for(int c=0; c<kol_vo; c++)
        cin >> v[0][c];
    for(int c=1; c<kol_vo; c++){
        for(int i=0; i<kol_vo-c; i++){
            v[c][i] = v[c-1][i] ^ v[c-1][i+1];
        }
    }/*
    cout << endl << endl;
    for(int c=0; c<kol_vo; c++){
        for(int i=0; i<kol_vo; i++)
            cout << v[c][i] << " ";
        cout << endl;
    }*/
    //cout << endl << endl;
    for(int l=kol_vo-1; l>=0; l--){
        for(int r=l; r<kol_vo; r++){
            dp[l][r] = max(v[r-l][l], (r-l>0?max(dp[l][r-1], dp[l+1][r]):0));
        }
    }/*
    cout << endl << endl;
    for(int c=0; c<kol_vo; c++){
        for(int i=0; i<kol_vo; i++)
            cout << dp[c][i] << " ";
        cout << endl;
    }
    cout << endl << endl;*/
    int ask;
    cin >> ask;
    for(int c=0; c<ask; c++){
        int l, r;
        cin >> l >> r;
        l--; r--;
        cout << dp[l][r] << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int long long
const int inf = 998244353;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
/*
    freopen("numbers.in", "r", stdin);
    freopen("numbers.out", "w", stdout);
*/

    int kol_vo;
    cin >> kol_vo;
    vector<vector<int> >  v(kol_vo, vector<int> (kol_vo));
    for(int c=0; c<kol_vo; c++)
        cin >> v[0][c];
    for(int c=1; c<kol_vo; c++){
        for(int i=0; i<kol_vo-c; i++){
            v[c][i] = v[c-1][i] ^ v[c-1][i+1];
        }
    }/*
    cout << endl << endl;
    for(int c=0; c<kol_vo; c++){
        for(int i=0; i<kol_vo; i++)
            cout << v[c][i] << " ";
        cout << endl;
    }*/
    //cout << endl << endl;
    vector<vector<int> > dp(kol_vo, vector<int> (kol_vo));
    for(int l=kol_vo-1; l>=0; l--){
        for(int r=l; r<kol_vo; r++){
            dp[l][r] = max(v[r-l][l], (r-l>0?max(dp[l][r-1], dp[l+1][r]):0));
        }
    }/*
    cout << endl << endl;
    for(int c=0; c<kol_vo; c++){
        for(int i=0; i<kol_vo; i++)
            cout << dp[c][i] << " ";
        cout << endl;
    }
    cout << endl << endl;*/
    int ask;
    cin >> ask;
    for(int c=0; c<ask; c++){
        int l, r;
        cin >> l >> r;
        l--; r--;
        cout << dp[l][r] << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define X first
#define Y second
#define sz(x) ((int)x.size())
#define CLR(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef pair<int,int> pii;
int n;
int a[5020];
vector<vector<int>>dp1,dp2;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	dp1.resize(n);
	dp2.resize(n);
	for(int i=0;i<n;i++)cin>>a[i];
	dp1[0].resize(n);
	for(int i=0;i<n;i++)dp1[0][i]=a[i];
	for(int i=1;i<n;i++)
	{
		dp1[i].resize(sz(dp1[i-1])-1);
		for(int j=0;j<dp1[i].size();j++)
			dp1[i][j]=dp1[i-1][j]^dp1[i-1][j+1];
	}
	dp2[0].resize(n);
	for(int i=0;i<n;i++)dp2[0][i]=a[i];
	for(int i=1;i<n;i++)
	{
		dp2[i].resize(sz(dp2[i-1])-1);
		for(int j=0;j<dp2[i].size();j++)
		{
			dp2[i][j]=max(dp2[i-1][j],dp2[i-1][j+1]);
			dp2[i][j]=max(dp1[i][j],dp2[i][j]);
		}
	}
	/*
	cout<<endl;
	for(int i=0;i<n;i++)
	{
		for(int j:dp1[i])cout<<j<<' ';
		cout<<endl;
	}
	cout<<endl;
	for(int i=0;i<n;i++)
	{
		for(int j:dp2[i])cout<<j<<' ';
		cout<<endl;
	}
	*/
	int q;
	cin>>q;
	while(q--)
	{
		int l,r;
		cin>>l>>r;
		l--;
		r--;
		cout<<dp2[r-l][l]<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define lson (root<<1)
#define rson ((root<<1)+1)
#define X first
#define Y second
#define sz(x) ((int)x.size())
#define CLR(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef pair<int,int> pii;
struct node
{
	int val=0;
};
struct TREE
{
	vector<node>tree;
	int *a;
	int n;
	void init()
	{
		tree.resize((n+2)<<2);
	}
	void build(int l,int r,int root)
	{
		if(l==r)tree[root].val=a[l];
		else
		{
			int mid=(l+r)/2;
			build(l,mid,lson);
			build(mid+1,r,rson);
			tree[root].val=max(tree[lson].val,tree[rson].val);
		}
	}
	int query(int l,int r,int root,int ql,int qr)
	{
		if(qr<l||ql>r)return -1;
		if(ql<=l&&qr>=r)return tree[root].val;
		int mid=(l+r)/2;
		return max(query(l,mid,lson,ql,qr),query(mid+1,r,rson,ql,qr));
	}
};
int n;
int p[5020],q[5020];
TREE tree[5020];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	int *now=p,*next=q;
	for(int i=0;i<n;i++)cin>>now[i];
	for(int i=0;i<n;i++)
	{
		tree[i+1].a=now;
		tree[i+1].n=n-i;
		tree[i+1].init();
		tree[i+1].build(0,n-i-1,1);
		for(int j=0;j<n-i-1;j++)
			next[j]=now[j]^now[j+1];
		swap(now,next);
	}
	int q;
	cin>>q;
	while(q--)
	{
		int l,r;
		cin>>l>>r;
		l--;
		r--;
		int ans=0;
		for(int i=1;i<=r-l+1;i++)
		{
			int tmp=tree[i].query(0,tree[i].n-1,1,l,r-i+1);
			ans=max(ans,tmp);
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
//#define endl '\n'
#define int long long
#define lson (root<<1)
#define rson ((root<<1)+1)
#define X first
#define Y second
#define sz(x) ((int)x.size())
#define CLR(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef pair<int,int> pii;
struct node
{
	int val=0;
};
struct TREE
{
	node tree[5003<<2];
	int *a;
	int n;
	void build(int l,int r,int root)
	{
		if(l==r)tree[root].val=a[l];
		else
		{
			int mid=(l+r)/2;
			build(l,mid,lson);
			build(mid+1,r,rson);
			tree[root].val=max(tree[lson].val,tree[rson].val);
		}
	}
	int query(int l,int r,int root,int ql,int qr)
	{
		if(qr<l||ql>r)return -1;
		if(ql<=l&&qr>=r)return tree[root].val;
		int mid=(l+r)/2;
		return max(query(l,mid,lson,ql,qr),query(mid+1,r,rson,ql,qr));
	}
};
int n;
int p[5020],q[5020];
TREE tree[5020];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	int *now=p,*next=q;
	for(int i=0;i<n;i++)cin>>now[i];
	for(int i=0;i<n;i++)
	{
		tree[i+1].a=now;
		tree[i+1].n=n-i;
		tree[i+1].build(0,n-i-1,1);
		for(int j=0;j<n-i-1;j++)
			next[j]=now[j]^now[j+1];
		swap(now,next);
	}
	int q;
	cin>>q;
	while(q--)
	{
		int l,r;
		cin>>l>>r;
		l--;
		r--;
		int ans=0;
		for(int i=1;i<=r-l+1;i++)
		{
			int tmp=tree[i].query(0,tree[i].n-1,1,l,r-i+1);
			ans=max(ans,tmp);
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=5005;
int n;
int vis[N][N];
int arr[N];
int dp[N][N];int ans[N][N];
int dpp(int i,int j)
{
    if(vis[i][j])
        return ans[i][j];
    if(i==j)
        return ans[i][i];
    vis[i][j]=1;
    ans[i][j]=max({ans[i][j],dpp(i+1,j),dpp(i,j-1)});
    return ans[i][j];
}
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
        dp[0][i]=arr[i];
        ans[i][i]=arr[i];
    }
    int len=1;
    int cursz=n;
    for(int i=1;i<=n-1;i++)
    {
        cursz--;
        for(int j=1;j<=cursz;j++)
        {
            int curnum=0;
            dp[i][j]=dp[i-1][j]^dp[i-1][j+1];
            ans[j][j+len]=dp[i][j];
        }
        len++;
    }
    int q;
    cin>>q;
    while(q--)
    {
    int l,r;
        cin>>l>>r;
        cout<<dpp(l,r)<<endl;
    }
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
   // cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
void solve()
{
   int n;
    cin>>n;
    int arr[n+1]={0};
    for(int i=1;i<=n;i++)
        cin>>arr[i];
    int cnt[n+1][33];
    memset(cnt,0,sizeof(cnt));
    for(int i=1;i<=n;i++)
    {
    for(int j=0;j<32;j++)
    {
        if(arr[i]&(1<<j))
            cnt[i][j]++;
    }
    }
    for(int i=0;i<32;i++)
        for(int j=1;j<=n;j++)
        cnt[j][i]+=cnt[j-1][i];
    int q;
    cin>>q;
    while(q--)
    {
    int l,r;
        cin>>l>>r;
        int ans=0;
        for(int i=0;i<32;i++)
        {
        int val=cnt[r][i]-cnt[l-1][i];
        if(val%2)
            ans|=(1<<i);
        }
        cout<<ans<<endl;
    }
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
   // cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
const int N=5005;
int n;
int vis[N][N];
int arr[N];
int dp[N][N];int ans[N][N];
int dpp(int i,int j)
{
    if(vis[i][j])
        return ans[i][j];
    if(i==j)
        return ans[i][i];
    vis[i][j]=1;
    ans[i][j]=max({ans[i][j],dpp(i+1,j),dpp(i,j-1)});
    return ans[i][j];
}
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
        dp[0][i]=arr[i];
        ans[i][i]=arr[i];
    }
    int len=1;
    int cursz=n;
    for(int i=1;i<=n-1;i++)
    {
        cursz--;
        for(int j=1;j<=cursz;j++)
        {
            int curnum=0;
            dp[i][j]=dp[i-1][j]^dp[i-1][j+1];
            ans[j][j+len]=dp[i][j];
        }
        len++;
    }
    int q;
    cin>>q;
    while(q--)
    {
    int l,r;
        cin>>l>>r;
        cout<<dpp(l,r)<<endl;
    }
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
   // cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include<iostream>
#include<iostream>
using namespace std;
const int N = 6000;
int num[N], f[N][N];
int max(int n, int m)
{
	return (m > n) ? m : n;
}
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> num[i];
		f[i][i] = num[i];
	}
	for (int l = 1; l < n; ++l)
		for (int i = 1; i + l <= n; ++i)
		{
			int k = 0;
			while ((1 << (k + 1)) < i)
				++k;
			f[i][i + l] = f[i][i + l - (1 << k)] ^ f[i + (1 << k)][i + l];
		}
	for (int l = 1; l < n; ++l)
		for (int i = 1; i + l <= n; ++i)
			f[i][i + l] = max(f[i][i + l], max(f[i][i + l - 1], f[i + 1][i + l]));
	int testnum, i, j;
	cin >> testnum;
	while (testnum--)
	{
		cin >> i >> j;
		cout << f[i][j] << endl;
	}
	return 0;
}


#include<iostream>
#include<iostream>
using namespace std;
const int N = 6000;
int num[N], f[N][N];
int max(int n, int m)
{
	return (m > n) ? m : n;
}
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> num[i];
		f[i][i] = num[i];
	}
	for (int l = 1; l < n; ++l)
		for (int i = 1; i + l <= n; ++i)
		{
			int k = 0;
			while ((1 << (k + 1)) < i)
				++k;
			f[i][i + l] = f[i][i + l - (1 << k)] ^ f[i + (1 << k)][i + l];
		}
	for (int i = 2; i <= n; ++i)
		for (int j = 1; j < i; ++j)
			f[j][i] = max(f[j][i], max(f[j + 1][i], f[j][i - 1]));
	int testnum, i, j;
	cin >> testnum;
	while (testnum--)
	{
		cin >> i >> j;
		cout << f[i][j] << endl;
	}
	return 0;
}


#include<iostream>
#include<iostream>
using namespace std;
const int N = 6000;
int num[N], f[N][N];
int max(int n, int m)
{
	return (m > n) ? m : n;
}
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> num[i];
		f[i][i] = num[i];
	}
	for (int l = 1; l < n; ++l)
		for (int i = 1; i + l <= n; ++i)
		{
			int k = 0;
			while (1 << (k + 1) < i)
				++k;
			f[i][i + l] = f[i][i + l - 1 << k] ^ f[i + 1 << k][i + l];
		}
	for (int i = 2; i <= n; ++i)
		for (int j = 1; j < i; ++j)
			f[j][i] = max(f[j][i], max(f[j + 1][i], f[j][i - 1]));
	int testnum, i, j;
	cin >> testnum;
	while (testnum--)
	{
		cin >> i >> j;
		cout << f[i][j] << endl;
	}
	return 0;
}


#include<iostream>
#include<iostream>
using namespace std;
const int N = 6000;
int num[N], f[N][N];
int max(int n, int m)
{
	return (m > n) ? m : n;
}
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> num[i];
		f[i][i] = num[i];
	}
	for (int l = 1; l < n; ++l)
		for (int i = 1; i + l <= n; ++i)
		{
			int k = 0;
			while ((1 << (k + 1)) < l)
				++k;
			f[i][i + l] = f[i][i + l - (1 << k)] ^ f[i + (1 << k)][i + l];
		}
	for (int l = 1; l < n; ++l)
		for (int i = 1; i + l <= n; ++i)
			f[i][i + l] = max(f[i][i + l], max(f[i][i + l - 1], f[i + 1][i + l]));
	int testnum, i, j;
	cin >> testnum;
	while (testnum--)
	{
		cin >> i >> j;
		cout << f[i][j] << endl;
	}
	return 0;
}


#include<iostream>
#include<iostream>
using namespace std;
const int N = 6000;
int num[N], f[N][N];
int max(int n, int m)
{
	return (m > n) ? m : n;
}
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> num[i];
		f[i][i] = num[i];
	}
	for (int l = 1; l < n; ++l)
		for (int i = 1; i + l <= n; ++i)
		{
			int k = 0;
			while (1 << (k + 1) < i)
				++k;
			f[i][i + l] = f[i][i + l - (1 << k)] ^ f[i + (1 << k)][i + l];
		}
	for (int i = 2; i <= n; ++i)
		for (int j = 1; j < i; ++j)
			f[j][i] = max(f[j][i], max(f[j + 1][i], f[j][i - 1]));
	int testnum, i, j;
	cin >> testnum;
	while (testnum--)
	{
		cin >> i >> j;
		cout << f[i][j] << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int>     pii;
typedef long long   ll;
typedef vector<ll>  vi;
typedef long double ld;
#define PI     3.14159265
#define int long long
#define fi first
#define se second
#define pb push_back
#define ins insert
#define mtup make_tuple
#define nl '\n'
#define mem(cont,data)    memset(cont, data, sizeof(cont))
#define all(a) a.begin(), a.end()
#define REP(i,s,e) for(int i = s; i <= e; i++)
#define REPR(i,s,e) for(int i = s; i >= e; i--)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define len(thing) (int)thing.size()
#define FLASH ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define maxheap  priority_queue<ll>
#define minheap  priority_queue<ll,vector<ll> ,greater<ll>>
#define d0(a) cout<<a<<' ';
#define d1(a) cout<<a<<nl;
#define d2(a,b) cout<<a<<' '<<b<<nl;
#define d3(a,b,c) cout<<a<<' '<<b<<' '<<c<<nl;
#define d4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<nl;
#define d5(a,b,c,d,e) cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<nl;
#define d6(a,b,c,d,e,f) cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<' '<<f<<nl;
const ll inf = 1e18 + 1;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 100;
int maxx(int a,int b){return max(a,b);}
int minn(int a,int b){return min(a,b);}
ll mpow(ll a,ll b,int mod = LLONG_MAX) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
template <typename T> bool cont(set<T> Set, T data){return Set.find(data) != Set.end();}
template <typename T> bool cont(multiset<T> Set, T data){return Set.find(data) != Set.end();}
int gcd(int a,int b) {return __gcd(a,b);}

void solve(){
  FLASH
  int n,q; cin >> n;
  int a[n+1], val[n+1][n+1], dp[n+1][n+1];
  REP(i,1,n) {
    cin >> a[i];
    dp[i][i] = val[i][i] = a[i];
  }
  REP(siz,2,n){
    int le=1,ri=siz;
    int vv = val[le][ri-1] ^ a[siz];
    val[le][ri] = vv;
    REP(i,2,n){
      int lo=i,hi=i+siz-1;
      if (hi>n)break;
      val[lo][hi] = vv ^ a[hi - siz] ^ a[hi];
      vv = val[lo][hi];
    }
  }
  REP(siz,2,n){
    REP(i,1,n){
      int lo=i,hi=i+siz-1;
      if(hi>n)break;
      dp[lo][hi] = maxx(val[lo][hi], maxx(val[lo+1][hi], val[lo][hi-1]));
    }
  }
  cin >> q;
  while(q--){
    int lo,hi;
    cin>>lo>>hi;
    cout << dp[lo][hi] << nl;
  }
}

signed main(){
  FLASH
  int testcases = 1;
  //cin>>testcases;
  while(testcases--) solve();
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int>     pii;
typedef long long   ll;
typedef vector<ll>  vi;
typedef long double ld;
#define PI     3.14159265
#define int long long
#define fi first
#define se second
#define pb push_back
#define ins insert
#define mtup make_tuple
#define nl '\n'
#define mem(cont,data)    memset(cont, data, sizeof(cont))
#define all(a) a.begin(), a.end()
#define REP(i,s,e) for(int i = s; i <= e; i++)
#define REPR(i,s,e) for(int i = s; i >= e; i--)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define len(thing) (int)thing.size()
#define FLASH ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define maxheap  priority_queue<ll>
#define minheap  priority_queue<ll,vector<ll> ,greater<ll>>
#define d0(a) cout<<a<<' ';
#define d1(a) cout<<a<<nl;
#define d2(a,b) cout<<a<<' '<<b<<nl;
#define d3(a,b,c) cout<<a<<' '<<b<<' '<<c<<nl;
#define d4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<nl;
#define d5(a,b,c,d,e) cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<nl;
#define d6(a,b,c,d,e,f) cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<' '<<f<<nl;
const ll inf = 1e18 + 1;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 100;
int maxx(int a,int b){return max(a,b);}
int minn(int a,int b){return min(a,b);}
ll mpow(ll a,ll b,int mod = LLONG_MAX) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
template <typename T> bool cont(set<T> Set, T data){return Set.find(data) != Set.end();}
template <typename T> bool cont(multiset<T> Set, T data){return Set.find(data) != Set.end();}
int gcd(int a,int b) {return __gcd(a,b);}

void solve(){
  FLASH
  int n,q; cin >> n;
  int a[n+1], val[n+1][n+1], dp[n+1][n+1];
  REP(i,1,n) {
    cin >> a[i];
    dp[i][i] = val[i][i] = a[i];
  }
  REP(siz,2,n){
    REP(i,1,n){
      int lo=i,hi=i+siz-1;
      if (hi>n)break;
      val[lo][hi] = val[lo+1][hi] ^ val[lo][hi-1];
    }
  }
  REP(siz,2,n){
    REP(i,1,n){
      int lo=i,hi=i+siz-1;
      if(hi>n)break;
      dp[lo][hi] = maxx(val[lo][hi], maxx(dp[lo+1][hi], dp[lo][hi-1]));
    }
  }
  cin >> q;
  while(q--){
    int lo,hi;
    cin>>lo>>hi;
    cout << dp[lo][hi] << nl;
  }
}

signed main(){
  FLASH
  int testcases = 1;
  //cin>>testcases;
  while(testcases--) solve();
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int>     pii;
typedef long long   ll;
typedef vector<ll>  vi;
typedef vector<vi>  vvi;
typedef long double ld;
#define PI     3.14159265
#define int long long
#define fi first
#define se second
#define pb push_back
#define ins insert
#define mtup make_tuple
#define nl '\n'
#define mem(cont,data)    memset(cont, data, sizeof(cont))
#define all(a) a.begin(), a.end()
#define REP(i,s,e) for(int i = s; i <= e; i++)
#define REPR(i,s,e) for(int i = s; i >= e; i--)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define len(thing) (int)thing.size()
#define FLASH ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define maxheap  priority_queue<ll>
#define minheap  priority_queue<ll,vector<ll> ,greater<ll>>
#define d0(a) cout<<a<<' ';
#define d1(a) cout<<a<<nl;
#define d2(a,b) cout<<a<<' '<<b<<nl;
#define d3(a,b,c) cout<<a<<' '<<b<<' '<<c<<nl;
#define d4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<nl;
#define d5(a,b,c,d,e) cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<nl;
#define d6(a,b,c,d,e,f) cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<' '<<f<<nl;
const ll inf = 1e18 + 1;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 100;
int maxx(int a,int b){return max(a,b);}
int minn(int a,int b){return min(a,b);}
ll mpow(ll a,ll b,int mod = LLONG_MAX) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
template <typename T> bool cont(set<T> Set, T data){return Set.find(data) != Set.end();}
template <typename T> bool cont(multiset<T> Set, T data){return Set.find(data) != Set.end();}
int gcd(int a,int b) {return __gcd(a,b);}

void solve(){
  FLASH
  int n,q; cin >> n;
  // int a[n+1], val[n+1][n+1], dp[n+1][n+1];
  vvi val(n+1,vi(n+1,0)), dp(n+1,vi(n+1,0));
  vi a(n+1);
  REP(i,1,n) {
    cin >> a[i];
    dp[i][i] = val[i][i] = a[i];
  }
  REP(siz,2,n){
    REP(i,1,n){
      int lo=i,hi=i+siz-1;
      if (hi>n)break;
      val[lo][hi] = val[lo+1][hi] ^ val[lo][hi-1];
    }
  }
  REP(siz,2,n){
    REP(i,1,n){
      int lo=i,hi=i+siz-1;
      if(hi>n)break;
      dp[lo][hi] = maxx(val[lo][hi], maxx(dp[lo+1][hi], dp[lo][hi-1]));
    }
  }
  cin >> q;
  while(q--){
    int lo,hi;
    cin>>lo>>hi;
    cout << dp[lo][hi] << nl;
  }
}

signed main(){
  FLASH
  int testcases = 1;
  //cin>>testcases;
  while(testcases--) solve();
  return 0;
}


/*
  Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;

inline ll read() {
    ll f = 1, x = 0;
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-')    f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f * x;
}

const int N = 5e3 + 10;

int dp[N][N], ans[N][N], n, m;

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    n = read();
    for(int i = 1; i <= n; i++) dp[i][i] = read();
    for(int len = 2; len <= n; len++) {
        for(int l = 1; l + len - 1 <= n; l++) {
            int r = l + len - 1;
            dp[l][r] = dp[l + 1][r] ^ dp[l][r - 1];
            ans[l][r] = max(dp[l][r], ans[l + 1][r] ^ ans[l][r - 1]);
        }
    }
    m = read();
    for(int i = 1; i <= m; i++) {
        int l = read(), r = read();
        cout << ans[l][r] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 5e3 + 10;
ll f[maxn][maxn];
ll dp[maxn][maxn];
ll a[maxn];
int n;
int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%lld", &a[i]);
		dp[i][i] = f[i][i] = a[i];
	}
	for(int len = 2; len <= n; len++){
		for(int l = 1, r;; l++){
			r = l+ len - 1; if(r > n) break;
			dp[l][r] = f[l][r] = f[l][r - 1] ^ f[l + 1][r];
			dp[l][r] = max(dp[l][r], dp[l][r - 1]);
			dp[l][r] = max(dp[l][r], dp[l + 1][r]);
		}
	}
	int q, l, r;
	scanf("%d", &q);
	while(q--){
		scanf("%d%d", &l, &r);
		printf("%lld\n", dp[l][r]);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=5009;
int a[maxn],n,dp[maxn][maxn],f[maxn][maxn];
int main()
{
	cin >> n;
	for(int i=1;i<=n;i++)	cin >> a[i],dp[i][i]=a[i],f[i][i]=a[i];
	for(int l=2;l<=n;l++)
	for(int i=1;i+l-1<=n;i++)
	{
		int j=i+l-1;
		dp[i][j] = (dp[i][j-1]^dp[i+1][j] );
		f[i][j]=max(f[i][j-1],f[i+1][j] );
		f[i][j]=max( f[i][j],dp[i][j] );
	}
	int q;
	cin >> q;
	while( q-- )
	{
		int l,r;
		cin >>l >> r;
		cout << f[l][r] << '\n';
	}
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define mem(a,x) memset(a,x,sizeof(a))
#define nl "\n"
#define fout(x) fixed<<setprecision(x)
#define one(x) __builtin_popcountll(x)
#define F first
#define S second
#define or_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// find_by_order(), order_of_key()

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << fout(10)<<x;}
void __print(double x) {cerr << fout(10)<<x;}
void __print(long double x) {cerr << fout(10)<<x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

const int mod=998244353;
const double eps=1e-9;
const double PI=acos(-1.0);

ll qpow(ll n,ll k){ll ans=1;assert(k>=0);n%=mod;while(k>0){if(k&1)ans=(ans*n)%mod;n=(n*n)%mod;k>>=1;}return ans%mod;}

// ****************************************************

int arr[5010];
int dp[5005][5005], x[5005][5005];
int Xor(int l, int r){
    if(l==r) return x[l][r]=arr[l];
    if(x[l][r]!=-1) return x[l][r];
    return x[l][r]=Xor(l, r-1)^Xor(l+1, r);
}
int DP(int l, int r){
    if(l==r) return dp[l][r]=arr[l];
    if(dp[l][r]!=-1) return dp[l][r];
    return dp[l][r]=max({Xor(l, r), DP(l, r-1), DP(l+1, r)});
}
int main()
{
    mem(dp, -1);
    mem(x, -1);
    int n;
    cin>>n;
    for(int i=1; i<=n; i++) scanf("%d", &arr[i]);
    int q;
    cin>>q;
    while(q--){
        int l, r;
        scanf("%d%d", &l, &r);
        printf("%d\n", DP(l, r));
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NMAX = 5111;
int n, m, cnt[NMAX], stop[NMAX], front[NMAX], back[NMAX];

int dist(int x, int y)
{
    if (y < x) y += n;
    return y - x;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    int src, dst;
    for (int i = 1; i <= m; ++i) {
        cin >> src >> dst;
        cnt[src]++;
        if (!stop[src]) {
            stop[src] = dist(src, dst);
        } else if (dist(src, dst) < stop[src]) {
            stop[src] = min(stop[src], dist(src, dst));
        }
    }
    for (int i = 1; i <= n; ++i) if (cnt[i]) {
        stop[i] += (cnt[i]-1) * n;
    }
    back[n] = stop[n];
    for (int i = n-1; i; --i) {
        back[i] = max(stop[i], back[i+1]+1);
    }
    for (int i = 1; i <= n; ++i) {
        front[i] += stop[i] + i;
    }
    for (int i = 1; i <= n; ++i) {
        front[i] = max(front[i], front[i-1]);
    }
    for (int i = 1; i <= n; ++i) {
        front[i] += n - i - 1;
    }
    for (int i = 1; i <= n; ++i) {
        cout << max(front[i-1], back[i]) << ' ';
    }
    cout << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int NMAX = 5111;
int n, m, cnt[NMAX], train[NMAX];

int dist(int x, int y)
{
    if (y < x) y += n;
    return y - x;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    int src, dst;
    for (int i = 1; i <= m; ++i) {
        cin >> src >> dst;
        cnt[src]++;
        if (!train[src]) {
            train[src] = dst;
        } else if (dist(src, dst) < dist(src, train[src])) {
            train[src] = dst;
        }
    }
    for (int i = 1; i <= n; ++i) {
        int mx = 0;
        for (int j = 1; j <= n; ++j) if (cnt[j]) {
            mx = max(mx, (cnt[j]-1)*n + dist(i, j) + dist(j, train[j]));
        }
        cout << mx << ' ';
    }
    cout << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()

signed main()  {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n);
    int val = 0;
    for (int i = 0; i < m; i++)  {
        int a, b;
        cin >> a >> b;
        a--, b--;
        if (b < a)  b += n;
        g[a].pb(b);
        val++;
    }
    for (int i = 0; i < n; i++)  {
        sort(all(g[i]));
    }
    for (int i = 0; i < n; i++)  {
        int ans = 0, pos = i, valgg = val;
        set<int> kek;
        vector<int> gg(n);
        for (int i = 0; i < n; i++)  gg[i] = g[i].size();
        while (valgg > 0)  {
            kek.erase(pos);
            if (gg[pos] - 1 >= 0)  {
                if (g[pos][gg[pos] - 1] < n)  kek.insert(g[pos][gg[pos] - 1]);
                else kek.insert(g[pos][gg[pos] - 1] - n);
                gg[pos]--;
                valgg--;
            }
            pos = (pos + 1) % n;
            ans++;
        }
        kek.erase(pos);
        while (!kek.empty())  {
            pos = (pos + 1) % n;
            kek.erase(pos);
            ans++;
        }
        cout << ans << ' ';
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//#define int long long
#define ll long long
#define double long double
#define pii pair<int, int>
#define vec vector
#define u_map unordered_map
#define u_set unordered_set
#define str string
#define all(el) el.begin(), el.end()
#define allr(el) el.rbegin(), el.rend()
#define sz(el) (int)el.size()
#define f first
#define s second
#define pb push_back
#define in insert
#define mp make_pair
#define endl "\n"

int maxi(int &a, int b) { if (b > a) { a = b; return 1; } return 0; }

int mini(int &a, int b) { if (b < a) { a = b; return 1; } return 0; }

int maxi(double &a, double b) { if (b > a) { a = b; return 1; } return 0; }

int mini(double &a, double b) { if (b < a) { a = b; return 1; } return 0; }

const int maxN = 5000;

vector<int> g[maxN];
int cnt[maxN];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        g[a].pb(b);
    }
    for (int i = 0; i < n; i++) {
        vector<pair<int, int>> tmp;
        for (int j = 0; j < sz(g[i]); j++) {
            tmp.pb({(g[i][j] + n - i) % n, g[i][j]});
        }
        sort(allr(tmp));
        g[i].clear();
        for (int j = 0; j < sz(tmp); j++) {
            g[i].pb(tmp[j].s);
        }
    }
    int need = 0;
    int mn = 0;
    for (int i = 0; i < n; i++) {
        need += sz(g[i]);
        maxi(mn, sz(g[i]) - 2);
    }
    for (int i = 0; i < n; i++) {
        int pos = i;
        int hv = 0;
        int ans = mn * n;
        for (int j = 0; j < n; j++) {
            cnt[j] = mn;
            hv += min(cnt[j], sz(g[j]));
        }
        unordered_set<int> nxt;
        while (hv < need) {
            if (cnt[pos] < sz(g[pos])) {
                nxt.in(g[pos][cnt[pos]++]);
                hv++;
            }
            pos = (pos + 1) % n;
            nxt.erase(pos);
            ans++;
        }
        if (sz(nxt) > 0) {
            int mx = 0;
            for (int now : nxt) {
                maxi(mx, (now + n - pos) % n);
            }
            ans += mx;
        }
        cout << ans << " ";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//#define int long long
#define ll long long
#define double long double
#define pii pair<int, int>
#define vec vector
#define u_map unordered_map
#define u_set unordered_set
#define str string
#define all(el) el.begin(), el.end()
#define allr(el) el.rbegin(), el.rend()
#define sz(el) (int)el.size()
#define f first
#define s second
#define pb push_back
#define in insert
#define mp make_pair
#define endl "\n"

int maxi(int &a, int b) { if (b > a) { a = b; return 1; } return 0; }

int mini(int &a, int b) { if (b < a) { a = b; return 1; } return 0; }

int maxi(double &a, double b) { if (b > a) { a = b; return 1; } return 0; }

int mini(double &a, double b) { if (b < a) { a = b; return 1; } return 0; }

const int maxN = 5000;

vector<int> g[maxN];
int cnt[maxN];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        g[a].pb(b);
    }
    for (int i = 0; i < n; i++) {
        vector<pair<int, int>> tmp;
        for (int j = 0; j < sz(g[i]); j++) {
            tmp.pb({(g[i][j] + n - i) % n, g[i][j]});
        }
        sort(allr(tmp));
        g[i].clear();
        for (int j = 0; j < sz(tmp); j++) {
            g[i].pb(tmp[j].s);
        }
    }
    int need = 0;
    for (int i = 0; i < n; i++) {
        need += sz(g[i]);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cnt[j] = 0;
        }
        int pos = i;
        int hv = 0;
        int ans = 0;
        unordered_set<int> nxt;
        while (hv < need) {
            if (cnt[pos] < sz(g[pos])) {
                nxt.in(g[pos][cnt[pos]++]);
                hv++;
            }
            pos = (pos + 1) % n;
            nxt.erase(pos);
            ans++;
        }
        if (sz(nxt) > 0) {
            int mx = 0;
            for (int now : nxt) {
                maxi(mx, (now + n - pos) % n);
            }
            ans += mx;
        }
        cout << ans << " ";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()

signed main()  {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n);
    int val = 0;
    for (int i = 0; i < m; i++)  {
        int a, b;
        cin >> a >> b;
        a--, b--;
        if (b < a)  b += n;
        g[a].pb(b);
        val++;
    }
    for (int i = 0; i < n; i++)  {
        sort(all(g[i]));
    }
    for (int i = 0; i < n; i++)  {
        int ans = 0, pos = i, valgg = val;
        unordered_set<int> kek;
        vector<int> gg(n);
        for (int i = 0; i < n; i++)  gg[i] = g[i].size();
        while (valgg > 0)  {
            kek.erase(pos);
            if (gg[pos] - 1 >= 0)  {
                if (g[pos][gg[pos] - 1] < n)  kek.insert(g[pos][gg[pos] - 1]);
                else kek.insert(g[pos][gg[pos] - 1] - n);
                gg[pos]--;
                valgg--;
            }
            pos = (pos + 1) % n;
            ans++;
        }
        kek.erase(pos);
        while (!kek.empty())  {
            pos = (pos + 1) % n;
            kek.erase(pos);
            ans++;
        }
        cout << ans << ' ';
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//#define int long long
#define ll long long
#define double long double
#define pii pair<int, int>
#define vec vector
#define u_map unordered_map
#define u_set unordered_set
#define str string
#define all(el) el.begin(), el.end()
#define allr(el) el.rbegin(), el.rend()
#define sz(el) (int)el.size()
#define f first
#define s second
#define pb push_back
#define in insert
#define mp make_pair
#define endl "\n"

int maxi(int &a, int b) { if (b > a) { a = b; return 1; } return 0; }

int mini(int &a, int b) { if (b < a) { a = b; return 1; } return 0; }

int maxi(double &a, double b) { if (b > a) { a = b; return 1; } return 0; }

int mini(double &a, double b) { if (b < a) { a = b; return 1; } return 0; }

const int maxN = 5000;

vector<int> g[maxN];
int cnt[maxN];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        g[a].pb(b);
    }
    for (int i = 0; i < n; i++) {
        vector<pair<int, int>> tmp;
        for (int j = 0; j < sz(g[i]); j++) {
            tmp.pb({(g[i][j] + n - i) % n, g[i][j]});
        }
        sort(allr(tmp));
        g[i].clear();
        for (int j = 0; j < sz(tmp); j++) {
            g[i].pb(tmp[j].s);
        }
    }
    int need = 0;
    for (int i = 0; i < n; i++) {
        need += sz(g[i]);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cnt[j] = 0;
        }
        int pos = i;
        int hv = 0;
        int ans = 0;
        set<int> nxt;
        while (hv < need) {
            if (cnt[pos] < sz(g[pos])) {
                nxt.in(g[pos][cnt[pos]++]);
                hv++;
            }
            pos = (pos + 1) % n;
            nxt.erase(pos);
            ans++;
        }
        if (sz(nxt) > 0) {
            int mx = 0;
            for (int now : nxt) {
                maxi(mx, (now + n - pos) % n);
            }
            ans += mx;
        }
        cout << ans << " ";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5000+555;
vector<int>V[maxn];
//vector<int>V2;
int n,m,a,b;
inline int dis(int f,int t,int s) {
	int ret2=s-f;
	if(ret2<0)ret2+=n;
	int ret=t-s;
	if(ret<0)ret+=n;
	return ret+ret2;
}
int main() {
//	ios::sync_with_stdio(false);
//	cin.tie(0);
//	cout.tie(0);
//	cin>>n>>m;
	int mx=0;
	scanf("%d%d",&n,&m);
	for(int i=0; i<m; i++)scanf("%d%d",&a,&b),V[a].push_back(b);
	for(int i=1; i<=n; i++)mx=max(mx,(int)V[i].size());
//	for(int i=1; i<=n; i++)if((int)V[i].size()==mx)V2.push_back(i);
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=n; j++) {
			if((int)V[j].size()<mx-1)continue;
			sort(V[j].begin(),V[j].end(),[&](int o1,int o2) {
				return dis(i,o1,j)<dis(i,o2,j);
			});
		}
		int ans=0;
		for(int j=1; j<=n; j++)if(!V[j].empty()&&!((int)V[j].size()<mx-1))ans=max(ans,((int)V[j].size()-1)*n+dis(i,V[j][0],j));
		printf("%d ",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5000+555;
vector<int>V[maxn];
vector<int>V2;
int n,m,a,b;
inline int dis(int f,int t,int s) {
	int ret2=s-f;
	if(ret2<0)ret2+=n;
	int ret=t-s;
	if(ret<0)ret+=n;
	return ret+ret2;
}
int main() {
//	ios::sync_with_stdio(false);
//	cin.tie(0);
//	cout.tie(0);
//	cin>>n>>m;
	int mx=0;
	scanf("%d%d",&n,&m);
	for(int i=0; i<m; i++)scanf("%d%d",&a,&b),V[a].push_back(b);
	for(int i=1; i<=n; i++)mx=max(mx,(int)V[i].size());
	for(int i=1; i<=n; i++)if((int)V[i].size()>=mx-1)V2.push_back(i);
	for(int i=1; i<=n; i++) {
		for(int j:V2) {
			sort(V[j].begin(),V[j].end(),[&](int o1,int o2) {
				return dis(i,o1,j)<dis(i,o2,j);
			});
		}
		int ans=0;
		for(int j:V2)ans=max(ans,((int)V[j].size()-1)*n+dis(i,V[j][0],j));
		printf("%d ",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5000+555;
vector<int>V[maxn];
vector<int>V2;
int n,m,a,b;
inline int dis(int f,int t,int s) {
	int ret2=s-f;
	if(ret2<0)ret2+=n;
	int ret=t-s;
	if(ret<0)ret+=n;
	return ret+ret2;
}
int main() {
	int mx=0;
	scanf("%d%d",&n,&m);
	for(int i=0; i<m; i++)scanf("%d%d",&a,&b),V[a].push_back(b);
	for(int i=1; i<=n; i++)mx=max(mx,(int)V[i].size());
	for(int i=1; i<=n; i++)if((int)V[i].size()>=mx-1&&!V[i].empty())V2.push_back(i);
	for(int i=1; i<=n; i++) {
		int ans=0;
		for(int j:V2)for(auto k:V[j])ans=max(ans,((int)V[j].size()-1)*n+dis(i,k,j));
		printf("%d ",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5000+555;
vector<int>V[maxn];
//vector<int>V2;
int n,m,a,b;
inline int dis(int f,int t,int s) {
	int ret2=s-f;
	if(ret2<0)ret2+=n;
	int ret=t-s;
	if(ret<0)ret+=n;
	return ret+ret2;
}
int main() {
//	ios::sync_with_stdio(false);
//	cin.tie(0);
//	cout.tie(0);
//	cin>>n>>m;
	int mx=0;
	scanf("%d%d",&n,&m);
	for(int i=0; i<m; i++)scanf("%d%d",&a,&b),V[a].push_back(b);
	for(int i=1; i<=n; i++)mx=max(mx,(int)V[i].size());
//	for(int i=1; i<=n; i++)if((int)V[i].size()==mx)V2.push_back(i);
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=n; j++) {
			if((int)V[j].size()<mx-1)continue;
			sort(V[j].begin(),V[j].end(),[&](int o1,int o2) {
				return dis(i,o1,j)<dis(i,o2,j);
			});
		}
		int ans=0;
		for(int j=1; j<=n; j++)if(!V[j].empty()&&!((int)V[j].size()<mx-1))ans=max(ans,((int)V[j].size()-1)*n+dis(i,V[j][0],j));
		cout<<ans<<' ';
	}
	cout<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5000+555;
vector<int>V[maxn];
vector<int>V2;
int n,m,a,b;
inline int dis(int f,int t,int s) {
	int ret2=s-f;
	if(ret2<0)ret2+=n;
	int ret=t-s;
	if(ret<0)ret+=n;
	return ret+ret2;
}
int main() {
//	ios::sync_with_stdio(false);
//	cin.tie(0);
//	cout.tie(0);
//	cin>>n>>m;
	int mx=0;
	scanf("%d%d",&n,&m);
	for(int i=0; i<m; i++)scanf("%d%d",&a,&b),V[a].push_back(b);
	for(int i=1; i<=n; i++)mx=max(mx,(int)V[i].size());
	for(int i=1; i<=n; i++)if((int)V[i].size()>=mx-1&&!V[i].empty())V2.push_back(i);
	for(int i=1; i<=n; i++) {
		for(int j:V2) {
			sort(V[j].begin(),V[j].end(),[&](int o1,int o2) {
				return dis(i,o1,j)<dis(i,o2,j);
			});
		}
		int ans=0;
		for(int j:V2)ans=max(ans,((int)V[j].size()-1)*n+dis(i,V[j][0],j));
		printf("%d ",ans);
	}
	return 0;
}