package main
import (
"bufio"
"fmt"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
scanner.Buffer(make([]byte, 1024*1024), 1024*1024*10)
nextInt := func() int {
scanner.Scan()
res := 0
for _, b := range scanner.Bytes() {
res = res*10 + int(b-'0')
}
return res
}
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
if !scanner.Scan() {
return
}
t := 0
for _, b := range scanner.Bytes() {
t = t*10 + int(b-'0')
}
for tc := 0; tc < t; tc++ {
n := nextInt()
a := make([]int, n+1)
for i := 1; i <= n; i++ {
a[i] = nextInt()
}
q := nextInt()
type Query struct {
L, id int
}
queries := make([][]Query, n+1)
for i := 0; i < q; i++ {
l := nextInt()
r := nextInt()
queries[r] = append(queries[r], Query{L: l, id: i})
}
ans := make([]int, q)
bit := make([]int, n+1)
for i := 1; i <= n; i++ {
bit[i] = 1 << 30
}
var active []int
for R := 1; R <= n; R++ {
curMin := 1 << 30
var nextActive []int
for i := len(active) - 1; i >= 0; i-- {
L := active[i]
val := a[L] | a[R]
if val < curMin {
curMin = val
for idx := L; idx > 0; idx -= idx & -idx {
if val < bit[idx] {
bit[idx] = val
}
}
}
if (a[R] & ^a[L]) != 0 {
nextActive = append(nextActive, L)
if len(nextActive) > 120 {
break
}
}
}
for i, j := 0, len(nextActive)-1; i < j; i, j = i+1, j-1 {
nextActive[i], nextActive[j] = nextActive[j], nextActive[i]
}
nextActive = append(nextActive, R)
active = nextActive
for _, qry := range queries[R] {
res := 1 << 30
for idx := qry.L; idx <= n; idx += idx & -idx {
if bit[idx] < res {
res = bit[idx]
}
}
ans[qry.id] = res
}
}
for i := 0; i < q; i++ {
if i > 0 {
fmt.Fprint(out, " ")
}
fmt.Fprint(out, ans[i])
}
fmt.Fprintln(out)
}
}