public static void main(String[] args) throws Throwable{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		for (String line; (line=in.readLine())!=null ; ) {
			int anthills = Integer.parseInt(line);
			if(anthills==0) break;

			con = new ArrayList[anthills];
			pes = new ArrayList[anthills];
			disRootANodo = new long[anthills];
			niveles = new int[anthills];
			papasDirectos = new int[anthills];
			secciones = new int[anthills];
			papasSecciones = new int[anthills];
			papasDirectos[0] = -1;

			for (int i = 0; i < anthills-1; i++) {
				ArrayList<Integer> lis ;
				ArrayList<Integer> p ;
				StringTokenizer st = new StringTokenizer(in.readLine());

				int iNodo = Integer.parseInt(st.nextToken());
				int iPeso = Integer.parseInt(st.nextToken());
				if( con[iNodo] == null )
					lis = new ArrayList<Integer>();
				else  	lis = con[iNodo];

				if( pes[iNodo]== null)
					p = new ArrayList<Integer>();
				else
					p = pes[iNodo];

				lis.add((i+1));
				p.add(iPeso);
				con[iNodo] = lis;
				papasDirectos[i+1] = iNodo;
				pes[iNodo] = p;
			}

			bfsRootANodo();

			int [] copy = niveles.clone();
			Arrays.sort(copy);
			int raizH = (int)( Math.ceil( Math.sqrt( (double)(copy[copy.length-1]) ) ) );
			for (int i = 0; i < niveles.length; i++) {
				secciones[i] = niveles[i]/raizH;
			}

			dfs(raizH);

			int query = Integer.parseInt(in.readLine());
			for (int i = 0; i < query; i++) {
				StringTokenizer st = new StringTokenizer(in.readLine());
				int nodo1 = Integer.parseInt(st.nextToken());
				int nodo2 = Integer.parseInt(st.nextToken());
				sb.append(solve(nodo1, nodo2));
				if( i<query-1 )
					sb.append(" ");
			}
			sb.append("\n");
		}
		System.out.print(new String(sb));
	}