using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp11
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Unesite N (broj banaka)");
            int N = Convert.ToInt32(Console.ReadLine());
            int[] bVr = new int[N];
            int[] bI = new int[N]; 
            int[] bPo = new int[N];
            int[] b1 = new int[N - 1];
            int[] b2 = new int[N - 1];
            Console.WriteLine("Unesite parove povezanih banaka");
            int temp1 = 0;
            for (int i = 0; i < N - 1; i++)
            {
                temp1 = Convert.ToInt32(Console.ReadLine());
                bPo[temp1 - 1]++;
                b1[i] = temp1;
                temp1 = Convert.ToInt32(Console.ReadLine());
                bPo[temp1 - 1]++;
                b2[i] = temp1;
                Console.WriteLine();
            }

            bool temp2 = false;
            for(int i = 0; i < N; i++)
            {
                if (bPo[i] == 1 && temp2 == true)
                {
                    bI[N - 1] = i + 1;
                }
                if (bPo[i] == 1 && temp2 == false)
                {
                    bI[0] = i + 1;
                    temp2 = true;
                }
            }

            for (int j = 0; j < N - 1; j++)
            {
                    if (bI[0] == b1[j])
                    {
                        bI[1] = b2[j];
                    break;
                    }
                    if (bI[0] == b2[j])
                    {
                        bI[1] = b1[j];
                    break;
                    }
            }

            for (int i = 2; i < N - 1; i++)
            {
                for (int j = 0; j < N - 1; j++)
                {
                    if (bI[i - 1] == b1[j] && b2[j] != bI[i - 2])
                    {
                        bI[i] = b2[j];
                    }
                    if (bI[i - 1] == b2[j] && b1[j] != bI[i - 2])
                    {
                        bI[i] = b1[j];
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine("Uneti pocetne vrednosti banaka");
            for (int k = 0; k < N; k++)
            {
                bVr[k] = Convert.ToInt32(Console.ReadLine());
            }

            Console.WriteLine();
            Console.WriteLine("Uneti Q (broj upita)");
            int Q = Convert.ToInt32(Console.ReadLine());
            int[] r = new int[Q + 1];


            int minVr = 0;
            int trVr = 0;
            int minI = 0;
            for(int j = 0; j < N; j++)
            {
                for (int i = 0; i < N - 1; i++)
                {
                    trVr += bVr[i] * Math.Abs(j - i);
                }
                if(trVr < minVr || minVr == 0)
                {
                    minI = j + 1;
                    minVr = trVr;
                }
                trVr = 0;
            }
            r[0] = minI;

            int temp3 = 0;
            minVr = 0;
            if (Q > 0)
            {
                for (int k = 0; k < Q; k++)
                {
                    Console.WriteLine("Uneti ostavljen novac i banku");
                    temp1 = Convert.ToInt32(Console.ReadLine());
                    temp3 = Convert.ToInt32(Console.ReadLine());
                    bVr[temp3 - 1] += temp1;
                    for (int j = 0; j < N; j++)
                    {
                        for (int i = 0; i < N - 1; i++)
                        {
                            trVr += bVr[i] * Math.Abs(j - i);
                        }
                        if (trVr < minVr || minVr == 0)
                        {
                            minI = j;
                            minVr= trVr;
                        }
                        trVr = 0;
                        minVr = 0;
                    }
                    r[k + 1] = minI;
                }
            }

            for(int i = 0; i < r.Length; i++)
            {
                Console.WriteLine(bI[r[i]]);
            }

            Console.ReadLine();
        }
    }
}