package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	vv := make([]int64, n+1)
	ww := make([]int64, n+1)
	for i := 0; i < n; i++ {
		vv[i+1] = getNextInt64(scanner)
		ww[i+1] = getNextInt64(scanner)
	}
	q := getNextInt(scanner)
	for i := 0; i < q; i++ {
		v := getNextInt(scanner)
		l := getNextInt64(scanner)
		ans := int64(0)
		bb := make([]int, 0)
		for b := v; b > 0; b /= 2 {
			bb = append(bb, b)
		}
		bl := len(bb)
		pp1 := make([]pair, 0)
		pp1 = append(pp1, pair{0, 0})
		for k := 0; k < bl/2; k++ {
			pp1 = update(ww[bb[k]], vv[bb[k]], pp1)
		}
		pp2 := make([]pair, 0)
		pp2 = append(pp2, pair{0, 0})
		for k := 0; k < bl-bl/2; k++ {
			pp2 = update(ww[bb[bl/2+k]], vv[bb[bl/2+k]], pp2)
		}

		rr := len(pp2) - 1
		for j := 0; j < len(pp1); j++ {
			for rr >= 0 && pp1[j][0]+pp2[rr][0] > l {
				rr--
			}
			if rr < 0 {
				break
			}
			if ans < pp1[j][1]+pp2[rr][1] {
				ans = pp1[j][1] + pp2[rr][1]
			}
		}
		fmt.Fprintln(writer, ans)
	}

}

type pair [2]int64

func update(w, v int64, pp []pair) []pair {
	n := len(pp)
	pp2 := make([]pair, n)
	pp3 := make([]pair, n<<1)
	for i := 0; i < n; i++ {
		pp2[i][0] = pp[i][0] + w
		pp2[i][1] = pp[i][1] + v
	}
	var i, j, k int
	for i < n || j < n {
		if i == n {
			pp3[k][0] = pp2[j][0]
			pp3[k][1] = max(pp3[maxint(0, k-1)][1], pp2[j][1])
			j++
			k++
			continue
		}
		if j == n {
			pp3[k][0] = pp[i][0]
			pp3[k][1] = max(pp3[maxint(0, k-1)][1], pp[i][1])
			i++
			k++
			continue
		}
		if pp[i][0] == pp2[j][0] {
			if pp[i][1] > pp2[j][1] {
				pp3[k][0] = pp2[j][0]
				pp3[k][1] = max(pp3[maxint(0, k-1)][1], pp2[j][1])
				j++
				k++
				continue
			}
			pp3[k][0] = pp[i][0]
			pp3[k][1] = max(pp3[maxint(0, k-1)][1], pp[i][1])
			i++
			k++
			continue
		}
		if pp[i][0] < pp2[j][0] {
			pp3[k][0] = pp[i][0]
			pp3[k][1] = max(pp3[maxint(0, k-1)][1], pp[i][1])
			i++
			k++
			continue
		}
		pp3[k][0] = pp2[j][0]
		pp3[k][1] = max(pp3[maxint(0, k-1)][1], pp2[j][1])
		j++
		k++
	}
	return pp3
}
func maxint(a, b int) int {
	if a < b {
		return b
	}
	return a
}
func max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}


Measured memory_bytes: 12940