macro_rules !read_value {($iter :expr ,($($t :tt ) ,*) ) =>{($(read_value !($iter ,$t ) ) ,*) } ;($iter :expr ,[$t :tt ;$len :expr ] ) =>{(0 ..$len ) .map (|_ |read_value !($iter ,$t ) ) .collect ::<Vec <_ >>() } ;($iter :expr ,{$t :tt =>$f :expr } ) =>{$f (read_value !($iter ,$t ) ) } ;($iter :expr ,chars ) =>{read_value !($iter ,String ) .chars () .collect ::<Vec <char >>() } ;($iter :expr ,usize1 ) =>{read_value !($iter ,usize ) -1 } ;($iter :expr ,$t :ty ) =>{$iter .next () .unwrap () .parse ::<$t >() .unwrap () } ;}
macro_rules !input_inner {($iter :expr ) =>{} ;($iter :expr ,) =>{} ;($iter :expr ,mut $var :ident :$t :tt $($r :tt ) *) =>{let mut $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,mut $var :ident $($r :tt ) *) =>{input_inner !{$iter ,mut $var :usize $($r ) *} } ;($iter :expr ,$var :ident :$t :tt $($r :tt ) *) =>{let $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,$var :ident $($r :tt ) *) =>{input_inner !{$iter ,$var :usize $($r ) *} } ;}
macro_rules !input {(source =$s :expr ,$($r :tt ) *) =>{let mut iter =$s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;(iter =$iter :ident ,$($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut $iter =s .split_whitespace () ;input_inner !{$iter ,$($r ) *} } ;($($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut iter =s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;}
#[macro_export]
macro_rules !min {($e :expr ) =>{$e } ;($e :expr ,$($es :expr ) ,+) =>{std ::cmp ::min ($e ,min !($($es ) ,+) ) } ;}
#[macro_export]
macro_rules !chmin {($dst :expr ,$($src :expr ) ,+) =>{{let x =std ::cmp ::min ($dst ,min !($($src ) ,+) ) ;$dst =x ;} } ;}
#[macro_export]
macro_rules !max {($e :expr ) =>{$e } ;($e :expr ,$($es :expr ) ,+) =>{std ::cmp ::max ($e ,max !($($es ) ,+) ) } ;}
#[macro_export]
macro_rules !chmax {($dst :expr ,$($src :expr ) ,+) =>{{let x =std ::cmp ::max ($dst ,max !($($src ) ,+) ) ;$dst =x ;} } ;}
fn main() {
    #[allow(unused_imports)]
    use std::io::Write as _;
    let __out = std::io::stdout();
    #[allow(unused_mut, unused_variables)]
    let mut __out = std::io::BufWriter::new(__out.lock());
    #[allow(unused_macros)]
    macro_rules !print {($($arg :tt ) *) =>(::std ::write !(__out ,$($arg ) *) .unwrap () ) }
    #[allow(unused_macros)]
    macro_rules !println {($($arg :tt ) *) =>(::std ::writeln !(__out ,$($arg ) *) .unwrap () ) }
    #[allow(unused_macros)]
    macro_rules! echo {
        ($iter :expr ) => {
            echo!($iter, "\n")
        };
        ($iter :expr ,$sep :expr ) => {
            let mut iter = $iter;
            if let Some(item) = iter.next() {
                print!("{}", item);
            }
            for item in iter {
                print!("{}{}", $sep, item);
            }
            println!();
        };
    }
    input!(n, vw: [(u32, u32); n], q, vl: [(usize, u32); q]);
    let th: usize = min!(1 << 12, n);
    let mut arr: Vec<Vec<_>> = vec![vec![]; th + 1];
    arr[0].push((0u32, 0u32));
    for i in 1..=th {
        let ln = arr[i / 2].len();
        arr[i].reserve(ln * 2);
        for j in 0..ln {
            let (ws, vs) = arr[i / 2][j];
            arr[i].push((ws, vs));
            arr[i].push((ws + vw[i - 1].1, vs + vw[i - 1].0));
        }
        arr[i].sort();
        for j in 1..arr[i].len() {
            chmax!(arr[i][j].1, arr[i][j - 1].1);
        }
    }
    for (u, l) in vl.into_iter() {
        if u <= th {
            let mut ans = 0;
            for i in 0..arr[u].len() {
                if arr[u][i].0 <= l {
                    chmax!(ans, arr[u][i].1);
                } else {
                    break;
                }
            }
            println!("{}", ans);
        } else {
            let mut xs = vec![(0u32, 0u32)];
            let mut p = u;
            while p > th {
                for i in 0..xs.len() {
                    let (ws, vs) = xs[i];
                    xs.push((ws + vw[p - 1].1, vs + vw[p - 1].0));
                }
                p = p / 2;
            }
            xs.sort();
            for j in 1..xs.len() {
                chmax!(xs[j].1, xs[j - 1].1);
            }
            let ys = &arr[p];
            let xn = xs.len();
            let yn = ys.len();
            let mut j = yn - 1;
            let mut ans = 0;
            'o: for i in 0..xn {
                while xs[i].0 + ys[j].0 > l {
                    if j == 0 {
                        break 'o;
                    }
                    j -= 1;
                }
                chmax!(ans, xs[i].1 + ys[j].1);
            }
            println!("{}", ans);
        }
    }
}


Measured memory_bytes: 97616