#[allow(dead_code)]
fn main() {
    let stdin = stdin();
    let mut reader = StdinReader::new(stdin.lock());
    let n = reader.u();
    let vw = reader.uv2(n);
    let q = reader.u();
    let vl = reader.uv2(q);

    // 根から深さ10までのすべてのナップサックをやっておく
    let mut knap = vec![Vec::new()];
    let m = min(1024, n);
    for i in 1..=m {
        knap.push(knapsack(&vw, i, 0));
    }

    let a = 1024;
    for (v, l) in vl {
        if v <= a {
            let k = &knap[v];
            println!("{}", k[l]);
        } else {
            let mut pattern = Vec::new();
            let mut v = v;
            while v > a {
                pattern.push(vw[v - 1]);
                v /= 2;
            }
            let p = 1 << (pattern.len());
            let mut ret = Vec::new();
            for i in 0..p {
                let mut v = 0;
                let mut w = 0;
                for j in 0..pattern.len() {
                    if i >> j & 1 == 0 {
                        v += pattern[j].0;
                        w += pattern[j].1;
                    }
                }
                ret.push((w, v));
            }
            //dbg!(&ret);
            let mut ans = 0;
            let k = &knap[v];
            for (w, v) in ret {
                if w > l {
                    continue;
                }
                ans = max(ans, k[l - w] + v);
            }
            println!("{}", ans);
        }
    }
    // dbg!(knap);
}

// Vec[重み] = 可能な価値の最大値 のVectorを返す全探索ナップサックを行う
fn knapsack(vw: &Vec<(usize, usize)>, mut l: usize, m: usize) -> Vec<usize> {
    let mut pattern = Vec::new();
    while l > m {
        pattern.push((vw[l - 1]));
        l /= 2;
    }

    let p = 1 << (pattern.len());
    let mut ret = vec![0; 100001];
    for i in 0..p {
        let mut v = 0;
        let mut w = 0;
        for j in 0..pattern.len() {
            if i >> j & 1 == 0 {
                v += pattern[j].0;
                w += pattern[j].1;
            }
        }
        if w <= 100000 {
            ret[w] = max(ret[w], v);
        }
    }
    let mut m = 0;
    for i in 0..100001 {
        ret[i] = max(ret[i], m);
        m = ret[i]
    }

    ret
}

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::*;
#[allow(unused_imports)]
use std::num::*;
#[allow(unused_imports)]
use std::str::*;

#[allow(unused_imports)]
use stdin_reader::StdinReader;

#[allow(dead_code)]
mod stdin_reader {
    use std::fmt::Debug;
    use std::io::*;
    use std::str::*;

    pub struct StdinReader<R: BufRead> {
        reader: R,
        buf: Vec<u8>,
        // Should never be empty
        pos: usize, // Should never be out of bounds as long as the input ends with '\n'
    }

    impl<R: BufRead> StdinReader<R> {
        pub fn new(r: R) -> StdinReader<R> {
            StdinReader {
                reader: r,
                buf: Vec::new(),
                pos: 0,
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T
        where
            T::Err: Debug,
        {
            if self.buf.is_empty() {
                self._read_next_line();
            }
            let mut start = None;
            loop {
                match (self.buf[self.pos], start.is_some()) {
                    (b' ', true) | (b'\n', true) => break,
                    (_, true) | (b' ', false) => self.pos += 1,
                    (b'\n', false) => self._read_next_line(),
                    (_, false) => start = Some(self.pos),
                }
            }
            let target = &self.buf[start.unwrap()..self.pos];
            from_utf8(target).unwrap().parse().unwrap()
        }

        fn _read_next_line(&mut self) {
            self.pos = 0;
            self.buf.clear();
            if self.reader.read_until(b'\n', &mut self.buf).unwrap() == 0 {
                panic!("Reached EOF");
            }
        }

        pub fn str(&mut self) -> String {
            self.next()
        }
        pub fn s(&mut self) -> Vec<char> {
            self.next::<String>().chars().collect()
        }
        pub fn i(&mut self) -> i64 {
            self.next()
        }
        pub fn i2(&mut self) -> (i64, i64) {
            (self.next(), self.next())
        }
        pub fn i3(&mut self) -> (i64, i64, i64) {
            (self.next(), self.next(), self.next())
        }
        pub fn u(&mut self) -> usize {
            self.next()
        }
        pub fn u2(&mut self) -> (usize, usize) {
            (self.next(), self.next())
        }
        pub fn u3(&mut self) -> (usize, usize, usize) {
            (self.next(), self.next(), self.next())
        }
        pub fn u4(&mut self) -> (usize, usize, usize, usize) {
            (self.next(), self.next(), self.next(), self.next())
        }
        pub fn u5(&mut self) -> (usize, usize, usize, usize, usize) {
            (
                self.next(),
                self.next(),
                self.next(),
                self.next(),
                self.next(),
            )
        }
        pub fn u6(&mut self) -> (usize, usize, usize, usize, usize, usize) {
            (
                self.next(),
                self.next(),
                self.next(),
                self.next(),
                self.next(),
                self.next(),
            )
        }
        pub fn f(&mut self) -> f64 {
            self.next()
        }
        pub fn f2(&mut self) -> (f64, f64) {
            (self.next(), self.next())
        }
        pub fn c(&mut self) -> char {
            self.next::<String>().pop().unwrap()
        }
        pub fn iv(&mut self, n: usize) -> Vec<i64> {
            (0..n).map(|_| self.i()).collect()
        }
        pub fn iv2(&mut self, n: usize) -> Vec<(i64, i64)> {
            (0..n).map(|_| self.i2()).collect()
        }
        pub fn iv3(&mut self, n: usize) -> Vec<(i64, i64, i64)> {
            (0..n).map(|_| self.i3()).collect()
        }
        pub fn uv(&mut self, n: usize) -> Vec<usize> {
            (0..n).map(|_| self.u()).collect()
        }
        pub fn uv2(&mut self, n: usize) -> Vec<(usize, usize)> {
            (0..n).map(|_| self.u2()).collect()
        }
        pub fn uv3(&mut self, n: usize) -> Vec<(usize, usize, usize)> {
            (0..n).map(|_| self.u3()).collect()
        }
        pub fn uv4(&mut self, n: usize) -> Vec<(usize, usize, usize, usize)> {
            (0..n).map(|_| self.u4()).collect()
        }
        pub fn fv(&mut self, n: usize) -> Vec<f64> {
            (0..n).map(|_| self.f()).collect()
        }
        pub fn cmap(&mut self, h: usize) -> Vec<Vec<char>> {
            (0..h).map(|_| self.s()).collect()
        }
    }
}


Measured memory_bytes: 811148