#![allow(unused_imports)]

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

use std::cmp::{min, max};
use std::collections::{HashMap, HashSet, VecDeque, BinaryHeap};

fn main() {
    input!{
        n: usize,
        v: [(usize, usize); n],
        q: usize,
        qs: [(usize1, usize); q]
    }
    let lmax = 100001;
    let mut b = 1;
    for _ in 0..9 {
        if b << 1 > n {
            b = n + 1;
            break;
        }
        b <<= 1;
    }
    let mut dp = vec![vec![0; lmax]; b - 1];
    for i in v[0].1..lmax {
        dp[0][i] = v[0].0;
    }
    for i in 1..b - 1 {
        let p = (i - 1) >> 1;
        let v0 = v[i].0;
        let v1 = v[i].1;
        for j in 0..v1 {
            dp[i][j] = dp[p][j];
        }
        for j in v1..lmax {
            dp[i][j] = max(max(dp[p][j], dp[i][j - 1]), dp[p][j - v1] + v0);
        }
    }
    for &(u, l) in &qs {
        if u < b - 1 {
            println!("{}", dp[u][l]);
            continue;
        }
        let mut m = u;
        let mut vs = vec![];
        while m >= b - 1 {
            vs.push(m);
            m = (m - 1) >> 1;
        }
        let mut ans = 0;
        let mut list = vec![];
        search(&mut vs, &v, &mut list);
        for &(val, w) in &list {
            if w <= l {
                ans = max(ans, val + dp[m][l - w]);
            }
        }
        println!("{}", ans);
    }
}

fn search(ps: &mut Vec<usize>, v: &Vec<(usize, usize)>, ret: &mut Vec<(usize, usize)>) {
    if let Some(x) = ps.pop() {
        search(ps, v, ret);
        for i in 0..ret.len() {
            ret.push((ret[i].0 + v[x].0, ret[i].1 + v[x].1));
        }
    } else {
        ret.push((0, 0));
    }
}


Measured memory_bytes: 408268