import scala.annotation.tailrec

object Main extends App {

  import scala.io.StdIn._
  import scala.math._

  val n = readLine.trim.toInt
  val expression = readLine.trim.toList match {
    case Expression(e) ⇒ e
    case _ ⇒ ???
  }
  for (_ ← 0 until readLine.trim.toInt){
    val Array(i, j) = readLine.trim.split(' ').map(_.toInt)
    println(
      expression.calculate(i, j).get
    )
  }
  type Length = Int
  sealed trait ExpressionNode {
    def whole: Int
    def length: Length
    def calculate(from: Int, to: Int): Option[Int]
  }
  trait LowPriority extends ExpressionNode
  trait NormalPriority extends LowPriority
  trait HighPriority extends NormalPriority

  object Number {
    def unapply(arg: List[Char]): Option[(NumberNode, List[Char])] = {
      arg match {
        case h::t if h.isDigit ⇒
          val (left, right) = arg.span(_.isDigit)
          Some(NumberNode(left.length, left.map(_.asDigit)), right)
        case _ ⇒ None
      }
    }
  }
  object Xor {
    def unapply(arg: List[Char]): Option[(XorNode, List[Char])] = {
      arg match {
        case left Number '^'::t ⇒ Some(parseXor(t, List(left)))
        case _ ⇒ None
      }
    }
    private def makeXor(array: Array[NumberNode], from: Int, until: Int): HighPriority = {
      if (from + 1 == until) array(from)
      else {
        val mid = (from + until) / 2
        new XorNode(makeXor(array, from, mid), makeXor(array, mid, until))
      }
    }
    private def makeAnd(result: List[NumberNode]): XorNode = {
      val array = result.reverse.toArray
      val mid = array.length / 2
      new XorNode(makeXor(array, 0, mid), makeXor(array, mid, array.length))
    }
    @tailrec
    private def parseXor(arg: List[Char], result: List[NumberNode]): (XorNode, List[Char]) = {
      arg match {
        case number Number '^'::rest ⇒ parseXor(rest, number::result)
        case number Number rest ⇒ (makeAnd(number::result), rest)
        case _ ⇒ (makeAnd(result), arg)
      }
    }
  }
  object HighPriority {
    def unapply(arg: List[Char]): Option[(HighPriority, List[Char])] = {
      arg match {
        case xor Xor rest ⇒ Some(xor, rest)
        case number Number rest ⇒ Some(number, rest)
        case _ ⇒ None
      }
    }
  }
  object And {
    def unapply(arg: List[Char]): Option[(AndNode, List[Char])] = {
      arg match {
        case high HighPriority '&'::t ⇒ Some(parseAnd(t, List(high)))
        case _ ⇒ None
      }
    }
    private def makeAnd(array: Array[HighPriority], from: Int, until: Int): NormalPriority = {
      if (from + 1 == until) array(from)
      else {
        val mid = (from + until) / 2
        new AndNode(makeAnd(array, from, mid), makeAnd(array, mid, until))
      }
    }
    private def makeAnd(result: List[HighPriority]): AndNode = {
      val array = result.reverse.toArray
      val mid = array.length / 2
      new AndNode(makeAnd(array, 0, mid), makeAnd(array, mid, array.length))
    }
    @tailrec
    private def parseAnd(arg: List[Char], result: List[HighPriority]): (AndNode, List[Char]) = {
      arg match {
        case high HighPriority '&'::t ⇒ parseAnd(t, high::result)
        case high HighPriority t ⇒ (makeAnd(high::result), t)
        case _ ⇒ (makeAnd(result), arg)
      }
    }
  }
  object NormalPriority {
    def unapply(arg: List[Char]): Option[(NormalPriority, List[Char])] = {
      arg match {
        case and And rest ⇒ Some(and, rest)
        case high HighPriority rest ⇒ Some(high, rest)
        case _ ⇒ None
      }
    }
  }
  object Or {
    def unapply(arg: List[Char]): Option[(OrNode, List[Char])] = {
      arg match {
        case normal NormalPriority '|'::t ⇒ Some(parseOr(t, List(normal)))
        case _ ⇒ None
      }
    }
    private def makeOr(array: Array[NormalPriority], from: Int, until: Int): ExpressionNode = {
      if (from + 1 == until) array(from)
      else {
        val mid = (from + until) / 2
        new OrNode(makeOr(array, from, mid), makeOr(array, mid, until))
      }
    }
    private def makeOr(result: List[NormalPriority]): OrNode = {
      val array = result.reverse.toArray
      val mid = array.length / 2
      new OrNode(makeOr(array, 0, mid), makeOr(array, mid, array.length))
    }
    private def parseOr(arg: List[Char], result: List[NormalPriority]): (OrNode, List[Char]) = {
      arg match {
        case normal NormalPriority '|'::t ⇒ parseOr(t, normal::result)
        case normal NormalPriority t ⇒ (makeOr(normal::result), t)
        case _ ⇒ (makeOr(result), arg)
      }
    }
  }
  object Expression{
    def unapply(arg: List[Char]): Option[ExpressionNode] = {
      arg match {
        case node Or Nil ⇒ Some(node)
        case normal NormalPriority Nil ⇒ Some(normal)
        case _ ⇒ None
      }
    }
  }
  case class XorNode(length: Length, left: HighPriority, right: HighPriority) extends ExpressionNode with HighPriority {
    def this(left: HighPriority, right: HighPriority){
      this(left.length + right.length + 1, left, right)
    }
    override val whole: Int = left.whole ^ right.whole
    override def calculate(from: Int, to: Int): Option[Int] = {
      if (from <= 0 && length < to) Some(whole)
      else if (to < 0 || length <= from) None
      else Some(left.calculate(from, to).getOrElse(0) ^ right.calculate(from - left.length - 1, to - left.length - 1).getOrElse(0))
    }
  }
  case class AndNode(length: Length, left: NormalPriority, right: NormalPriority) extends ExpressionNode with NormalPriority {
    def this(left: NormalPriority, right: NormalPriority){
      this(left.length + right.length + 1, left, right)
    }
    override val whole: Int = left.whole & right.whole
    override def calculate(from: Int, to: Int): Option[Int] = {
      if (from <= 0 && length < to) Some(whole)
      else if (to < 0 || length <= from) None
      else Some(left.calculate(from, to).getOrElse(-1) & right.calculate(from - left.length - 1, to - left.length - 1).getOrElse(-1))
    }
  }
  case class OrNode(length: Length, left: ExpressionNode, right: ExpressionNode) extends ExpressionNode with LowPriority {
    def this(left: ExpressionNode, right: ExpressionNode){
      this(left.length + right.length + 1, left, right)
    }
    override val whole: Int = left.whole | right.whole
    override def calculate(from: Int, to: Int): Option[Int] = {
      if (from <= 0 && length < to) Some(whole)
      else if (to < 0 || length <= from) None
      else Some(left.calculate(from, to).getOrElse(0) | right.calculate(from - left.length - 1, to - left.length - 1).getOrElse(0))
    }
  }
  case class NumberNode(length: Length, value: List[Int]) extends ExpressionNode with HighPriority {
    override val whole: Int = value.foldLeft(0){case (l, r) ⇒ l * 10 + r}
    override def calculate(from: Int, to: Int): Option[Int] = if (length <= from || to < 0) None else if (from <= 0 && length < to) Some(whole) else Some(read(value, from, to))
    private def read(arg: List[Int], from: Int, to: Int, result: Int = 0): Int = {
      arg match {
        case h::t if from <= 0 && 0 <= to ⇒ read(t, from - 1, to - 1, result * 10 + h)
        case _::t if 0 < from ⇒ read(t, from - 1, to - 1, 0)
        case _ if to < 0 ⇒ result
        case Nil ⇒ result
      }
    }
  }

  implicit class Extension[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}


Measured memory_bytes: 282832