For an array b of length m we define the function f as 

 f(b) = \begin{cases} b[1] &   if  m = 1 \\\ f(b[1] ⊕ b[2],b[2] ⊕ b[3],...,b[m-1] ⊕ b[m]) &   otherwise, \end{cases}  

where ⊕ is [bitwise exclusive OR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

For example, f(1,2,4,8)=f(1⊕2,2⊕4,4⊕8)=f(3,6,12)=f(3⊕6,6⊕12)=f(5,10)=f(5⊕10)=f(15)=15

You are given an array a and a few queries. Each query is represented as two integers l and r. The answer is the maximum value of f on all continuous subsegments of the array a_l, a_{l+1}, …, a_r.

Input

The first line contains a single integer n (1 ≤ n ≤ 5000) — the length of a.

The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2^{30}-1) — the elements of the array.

The third line contains a single integer q (1 ≤ q ≤ 100 000) — the number of queries.

Each of the next q lines contains a query represented as two integers l, r (1 ≤ l ≤ r ≤ n).

Output

Print q lines — the answers for the queries.

Examples

Input

3
8 4 1
2
2 3
1 2


Output

5
12


Input

6
1 2 4 8 16 32
4
1 6
2 5
3 4
1 2


Output

60
30
12
3

Note

In first sample in both queries the maximum value of the function is reached on the subsegment that is equal to the whole segment.

In second sample, optimal segment for first query are [3,6], for second query — [2,5], for third — [3,4], for fourth — [1,2].
/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
*/
import java.util.*;
import java.io.*;

   public class x983B
   {
      public static void main(String args[]) throws Exception
      {
         //BufferedReader infile = new BufferedReader(new FileReader("cowdate.in"));
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int N = Integer.parseInt(st.nextToken());
         int[] arr = readArr(N, infile, st);
         //sliding dp
         int[][] dp = new int[N][N];
         for(int i=0; i < N; i++)
            dp[i][i] = arr[i];
         for(int len=2; len <= N; len++)
            for(int a=0; a < N-len+1; a++)
            {
               int b = a+len-1;
               dp[a][b] = Math.max(dp[a][b], dp[a][b-1]^dp[a+1][b]);
            }
         int[][] dpUp = new int[N][N];
         for(int i=0; i < N; i++)
            dpUp[i][i] = arr[i];
         for(int len=2; len <= N; len++)
            for(int a=0; a < N-len+1; a++)
            {
               int b = a+len-1;
               dpUp[a][b] = Math.max(dpUp[a+1][b], dpUp[a][b-1]);
               dpUp[a][b] = Math.max(dpUp[a][b], dp[a][b]);
            }
         int Q = Integer.parseInt(infile.readLine());
         StringBuilder sb = new StringBuilder();
         for(int i=0; i < Q; i++)
         {
            st = new StringTokenizer(infile.readLine());
            int L = Integer.parseInt(st.nextToken());
            int R = Integer.parseInt(st.nextToken());
            sb.append(dpUp[L-1][R-1]+"\n");
         }
         System.out.print(sb.toString());
      }
      //input shenanigans
      public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
      {
         int[] arr = new int[N];
         st = new StringTokenizer(infile.readLine());
         for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(st.nextToken());
         return arr;
      }
      public static long[] readArr2(int N, BufferedReader infile, StringTokenizer st) throws Exception
      {
         long[] arr = new long[N];
         st = new StringTokenizer(infile.readLine());
         for(int i=0; i < N; i++)
            arr[i] = Long.parseLong(st.nextToken());
         return arr;
      }
      public static boolean isPrime(long n) 
      {
          if(n < 2) return false;
          if(n == 2 || n == 3) return true;
          if(n%2 == 0 || n%3 == 0) return false;
          long sqrtN = (long)Math.sqrt(n)+1;
          for(long i = 6L; i <= sqrtN; i += 6) {
              if(n%(i-1) == 0 || n%(i+1) == 0) return false;
          }
          return true;
      }
      public static long gcd(long a, long b)
      {
         //find gcd
         if(a > b)
         {
            long t = a;
            a = b;
            b = t;
         }
         if(a == 0L)
            return b;
         return gcd(b%a, a);
      }
      public static ArrayList<Integer> findDiv(int N)
      {
         //gens all divisors of N
         ArrayList<Integer> ls1 = new ArrayList<Integer>();
         ArrayList<Integer> ls2 = new ArrayList<Integer>();
         for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++)
            if(N%i == 0)
            {
               ls1.add(i);
               ls2.add(N/i);
            }
         Collections.reverse(ls2);
         for(int b: ls2)
            if(b != ls1.get(ls1.size()-1))
               ls1.add(b);
         return ls1;
      }
      public static void dfs(int curr, LinkedList<Integer>[] edges, boolean[] seen)
      {
         seen[curr] = true;
         for(int next: edges[curr])
            if(!seen[next])
               dfs(next, edges, seen);
      }
      public static void sort(int[] arr)
      {
         //stable heap sort
         PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
         for(int a: arr)
            pq.add(a);
         for(int i=0; i < arr.length; i++)
            arr[i] = pq.poll();
      }
      public static long power(long x, long y, long p) 
      { 
          long res = 1L;      
          x = x % p;  
          while (y > 0) 
          { 
              if((y & 1)==1) 
                  res = (res * x) % p; 
              y = y >> 1;  
              x = (x * x) % p;  
          } 
          return res; 
      } 
   }
   class DSU
   {
      public int[] dsu;
      public int[] size;
      
      public DSU(int[] a, int[] b)
      {
         dsu = a;
         size = b;
      }
      public int find(int x, int[] dsu) 
      {
   		return dsu[x] == x ? x : (dsu[x] = find(dsu[x],dsu));
   	}
      public void merge(int x, int y, int[] dsu) 
      {
   		int fx = find(x,dsu);
   		int fy = find(y,dsu);
   		dsu[fx] = fy;
   	}
   	public void merge(int x, int y, int[] dsu, int[] size) 
      {
   		int fx = find(x,dsu);
   		int fy = find(y,dsu);
   		size[fy] += size[fx];
   		dsu[fx] = fy;
   	}
   }
   class Point implements Comparable<Point>
   {
      public int x;
      public int y;
      public Point(int a, int b)
      {
         x = a;
         y = b;
      }
      public int compareTo(Point oth)
      {
         return x-oth.x;
      }
   }
   class FenwickTree
   {
      //1 indexed
      public int[] tree;
      public int size;
      
      public FenwickTree(int size)
      {
         this.size = size;
         tree = new int[size+5];
      }
      public void add(int i, int v)
      {
         while(i <= size)
         {
            tree[i] += v;
            i += i&-i;
         }
      }
      public int find(int i)
      {
         int res = 0;
         while(i >= 1)
         {
            res += tree[i];
            i -= i&-i;
         }
         return res;
      }
   }
   class RangeTree 
   {
		private long[] min;
		private long[] lazy;
		private long[] sum;
		private int size;
		public RangeTree(int size) 
      {
			this.size = size;
			min = new long[4*size];
			sum = new long[4*size];
			lazy = new long[4*size];
		}
		public void update(int l, int r, int inc) 
      {
			update(1, 0, size-1, l, r, inc);
		}
		private void pushDown(int index, int l, int r) 
      {
			min[index] += lazy[index];
			sum[index] += lazy[index] * (r-l+1);
			if(l != r) 
         {
				lazy[2*index] += lazy[index];
				lazy[2*index+1] += lazy[index];
			}
			lazy[index] = 0;
		}
		private void pullUp(int index, int l, int r) 
      {
			int m = (l+r)/2;
			min[index] = Math.min(evaluateMin(2*index, l, m), evaluateMin(2*index+1, m+1, r));
			sum[index] = evaluateSum(2*index, l, m) + evaluateSum(2*index+1, m+1, r);
		}
		private long evaluateSum(int index, int l, int r) 
      {
			return sum[index] + (r-l+1)*lazy[index];
		}
		private long evaluateMin(int index, int l, int r) 
      {
			return min[index] + lazy[index];
		}
		private void update(int index, int l, int r, int left, int right, int inc) 
      {
			if(r < left || l > right) return;
			if(l >= left && r <= right) {
				lazy[index] += inc;
				return;
			}
			pushDown(index, l, r);
			int m = (l+r)/2;
			update(2*index, l, m, left, right, inc);
			update(2*index+1, m+1, r, left, right, inc);
			pullUp(index, l, r);
		}
		public long minQuery(int l, int r) 
      {
			return minQuery(1, 0, size-1, l, r);
		}
		private long minQuery(int index, int l, int r, int left, int right) 
      {
			if(r < left || l > right) return Long.MAX_VALUE;
			if(l >= left && r <= right) {
				return evaluateMin(index, l, r);
			}
			pushDown(index, l, r);
			int m = (l+r)/2;
			long ret = Long.MAX_VALUE;
			ret = Math.min(ret, minQuery(2*index, l, m, left, right));
			ret = Math.min(ret, minQuery(2*index+1, m+1, r, left, right));
			pullUp(index, l, r);
			return ret;
		}
		public long sumQuery(int l, int r) 
      {
			return sumQuery(1, 0, size-1, l, r);
		}
		private long sumQuery(int index, int l, int r, int left, int right) 
      {
			if(r < left || l > right) return 0;
			if(l >= left && r <= right) {
				return evaluateSum(index, l, r);
			}
			pushDown(index, l, r);
			int m = (l+r)/2;
			long ret = 0;
			ret += sumQuery(2*index, l, m, left, right);
			ret += sumQuery(2*index+1, m+1, r, left, right);
			pullUp(index, l, r);
			return ret;
		}
	}
   class Loc implements Comparable<Loc>
   {
      public int val;
      public int index;
      
      public Loc(int a, int b)
      {
         index = a;
         val = b;
      }
      public int compareTo(Loc oth)
      {
         return val-oth.val;
      }
   }
   class BitSet
   {
      public long[] sets;
      public int size;
      
      public BitSet(int N)
      {
         size = N;
         if(N%64 == 0)
            sets = new long[N/64];
         else
            sets = new long[N/64+1];
      }
      public void add(int i)
      {
         int dex = i/64;
         int thing = i%64;
         sets[dex] |= (1L << thing);
      }
      public int and(BitSet oth)
      {
         int boof = Math.min(sets.length, oth.sets.length);
         int res = 0;
         for(int i=0; i < boof; i++)
            res += Long.bitCount(sets[i] & oth.sets[i]);
         return res;
      }
      public int xor(BitSet oth)
      {
         int boof = Math.min(sets.length, oth.sets.length);
         int res = 0;
         for(int i=0; i < boof; i++)
            res += Long.bitCount(sets[i] ^ oth.sets[i]);
         return res;
      }
   }
   //use for codeforces!