text: class INPUT:
    def __init__(self):
      self.l=open(0).read().split()[::-1]
      self.length=len(self.l)
      return
  
    def stream(self,k=1,f=int,f2=False):
      assert(-1<k)
      m=self.length
      if m==0 or m<k:
        raise Exception("There is no input!")
      elif f!=str:
        if k==0:
          self.length=0
          return list(map(f,self.l[::-1]))
        if k==1 and not f2:
          self.length-=1
          return f(self.l.pop())
        if k==1 and f2:
          self.length-=1
          return [f(self.l.pop())]
        ret=[]
        for _ in [0]*k:
          ret.append(f(self.l.pop()))
        self.length-=k
        return ret
      else:
        if k==0:
          self.length=0
          return self.l[::-1]
        if k==1 and not f2:
          self.length-=1
          return self.l.pop()
        if k==1 and f2:
          self.length-=1
          return [self.l.pop()]
        ret=[]
        for _ in [0]*k:
          ret.append(self.l.pop())
        self.length-=k
        return ret
  pin=INPUT().stream
  
  """
  pin(number[default:1],f[default:int],f2[default:False])
  if number==0 -> return left all
  listを変数で受け取るとき、必ずlistをTrueにすること。
  """
  
  def main():
    d1=[0]
    d2=[0]
    t1=0
    t2=0
    N=pin(1)
    for _ in[0]*N:
      c,p=pin(2)
      if c==1:
        t1+=p
      else:
        t2+=p
      d1.append(t1)
      d2.append(t2)
    for _ in[0]*pin(1):
      L,R=pin(2)
      print(d1[R]-d1[L-1],d2[R]-d2[L-1])
    return
  
  main()
  