fn main() {
	let mut s = String::new();
	use std::io::Read;
	std::io::stdin().read_to_string(&mut s).unwrap();
	let mut s = s.split_whitespace();
	let n: usize = s.next().unwrap().parse().unwrap();
	let mut a = Vec::with_capacity(n + 1);
	for _ in 0..n {
		let v: i32 = s.next().unwrap().parse().unwrap();
		let w: i32 = s.next().unwrap().parse().unwrap();
		a.push((v, w));
	}
	let m = (n + 1).next_power_of_two().trailing_zeros();
	let half = m / 2;
	let mut memo = vec![None; (1 << half) >> 1];
	let q: usize = s.next().unwrap().parse().unwrap();

	let get_ancestors = |v: usize, stop: usize| {
		let mut r = vec![];
		let mut i = v;
		while i > stop {
			r.push(a[i - 1]);
			i /= 2;
		}
		r
	};

	use std::io::Write;
	let out = std::io::stdout();
	let mut out = std::io::BufWriter::new(out.lock());
	for _ in 0..q {
		let v: usize = s.next().unwrap().parse().unwrap();
		let l: i32 = s.next().unwrap().parse().unwrap();
		let depth = (v + 1).next_power_of_two().trailing_zeros();
		let ans = if depth >= half && half > 0 {
			// deep
			let x = v >> (depth - half);
			let memo = if let Some(r) = memo[x - (1 << (half - 1))].as_ref() {
				r
			} else {
				let a = get_ancestors(x, 0);

				let mut r = vec![];
				for i in 0..1 << a.len() {
					let mut v = 0;
					let mut w = 0;
					for j in 0..a.len() {
						if i & (1 << j) != 0 {
							v += a[j].0;
							w += a[j].1;
						}
					}
					r.push((w, v));
				}
				r.sort();
				let mut t = vec![(0, 0)];
				for &r in r[1..].iter() {
					if t.last().unwrap().1 < r.1 {
						t.push(r);
					}
				}
				memo[x - (1 << (half - 1))] = Some(t);
				memo[x - (1 << (half - 1))].as_ref().unwrap()
			};

			if v == x {
				memo[memo.upper_bound_by_key(&l, |a| a.0) - 1].1
			} else {
				let mut b = Vec::with_capacity(1 << (depth - half));
				let a = get_ancestors(v, x);
				for i in 0..1 << a.len() {
					let mut v = 0;
					let mut w = 0;
					for j in 0..a.len() {
						if i & (1 << j) != 0 {
							v += a[j].0;
							w += a[j].1;
						}
					}
					if w <= l {
						b.push((w, v));
					}
				}
				b.sort();
				let mut i = memo.len() - 1;
				let mut ans = 0;
				for (w, v) in b {
					while i > 0 && memo[i].0 + w > l {
						i -= 1;
					}
					if ans < v + memo[i].1 {
						ans = v + memo[i].1;
					}
				}
				ans
			}
		} else {
			// shallow
			let a = get_ancestors(v, 0);
			let mut ans = 0;
			for i in 0..1 << a.len() {
				let mut v = 0;
				let mut w = 0;
				for j in 0..a.len() {
					if i & (1 << j) != 0 {
						v += a[j].0;
						w += a[j].1;
					}
				}
				if w <= l && ans < v {
					ans = v;
				}
			}
			ans
		};
		writeln!(out, "{}", ans).unwrap();
	}
}

pub trait BinarySearch<'a, T: 'a> {
	fn upper_bound_by_key<B, F>(&'a self, b: &B, f: F) -> usize
	where
		B: Ord,
		F: Fn(&'a T) -> B;
}

impl<'a, T: 'a + Ord> BinarySearch<'a, T> for [T] {
	fn upper_bound_by_key<B, F>(&'a self, b: &B, f: F) -> usize
	where
		B: Ord,
		F: Fn(&'a T) -> B,
	{
		let mut first = 0;
		let mut len = self.len() - first;
		while len > 0 {
			let half = len >> 1;
			let mid = first + half;
			use std::cmp::Ordering;
			match f(&self[mid]).cmp(b) {
				Ordering::Less | Ordering::Equal => {
					first = mid + 1;
					len = len - half - 1;
				}
				Ordering::Greater => {
					len = half;
				}
			}
		}
		first
	}
}


Measured memory_bytes: 9060