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 '^'::(right Xor rest) ⇒ Some(Xor(left, right), rest)
        case left Number '^'::(right Number rest) ⇒ Some(Xor(left, right), rest)
        case _ ⇒ None
      }
    }
    def apply(left: HighPriority, right: HighPriority): XorNode = {
      if (left.length * 2 < right.length){
        right match {
          case XorNode(_, l, r) ⇒ XorNode(left.length + right.length + 1, Xor(left, l), r)
          case _ ⇒ XorNode(left.length + right.length + 1, left, right)
        }
      }else if (right.length * 2 < left.length){
        left match {
          case XorNode(_, l, r) ⇒ XorNode(left.length + right.length + 1, l, Xor(r, right))
          case _ ⇒ XorNode(left.length + right.length + 1, left, right)
        }
      }else XorNode(left.length + right.length + 1, left, right)
    }
  }
  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 left HighPriority '&'::(right And rest) ⇒ Some(And(left, right), rest)
        case left HighPriority '&'::(right HighPriority rest) ⇒ Some(And(left, right), rest)
        case _ ⇒ None
      }
    }
    def apply(left: NormalPriority, right: NormalPriority): AndNode = {
      if (left.length * 2 < right.length){
        right match {
          case AndNode(_, l, r) ⇒ AndNode(left.length + right.length + 1, And(left, l), r)
          case _ ⇒ AndNode(left.length + right.length + 1, left, right)
        }
      }else if (right.length * 2 < left.length){
        left match {
          case AndNode(_, l, r) ⇒ AndNode(left.length + right.length + 1, l, And(r, right))
          case _ ⇒ AndNode(left.length + right.length + 1, left, right)
        }
      }else AndNode(left.length + right.length + 1, left, right)
    }
  }
  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 left NormalPriority '|'::(right Or rest) ⇒ Some(Or(left, right), rest)
        case left NormalPriority '|'::(right NormalPriority rest) ⇒ Some(Or(left, right), rest)
        case _ ⇒ None
      }
    }
    def apply(left: ExpressionNode, right: ExpressionNode): OrNode = {
      if (left.length * 2 < right.length){
        right match {
          case OrNode(_, l, r) ⇒ OrNode(left.length + right.length + 1, Or(left, l), r)
          case _ ⇒ OrNode(left.length + right.length + 1, left, right)
        }
      }else if (right.length * 2 < left.length){
        left match {
          case OrNode(_, l, r) ⇒ OrNode(left.length + right.length + 1, l, Or(r, right))
          case _ ⇒ OrNode(left.length + right.length + 1, left, right)
        }
      }else OrNode(left.length + right.length + 1, left, right)
    }
  }
  object Expression{
    def unapply(arg: List[Char]): Option[ExpressionNode] = {
      arg match {
        case node Or Expression(e) ⇒ Some(OrNode(node.length + e.length + 1, node, e))
        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 {
    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 {
    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 {
    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)
  }
}
