The Staging

Given n gangsters each aiming at a distinct target, count survivors after each of q updates to the shooting time of one gangster.

Hard8GraphDynamic programmingSortingImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

Steven Byteberg is a movie director who specializes in action movies. He is now working on a new movie about the Byteonian Mafia wars. Byteberg wonders what the climax of the movie, a spectacular exchange of gunfire, should look like.

nn gangsters take part in this scene. For simplicity they are numbered from 1 to nn. When the tension reaches its peak, every gangster draws his weapon and takes aim at another gangster. Nobody is aimed at by more than one gangster. The gangsters are poor but well trained: each of them can shoot only once, and that shot is always accurate and always deadly.

At some point one of the thugs cannot stand the tension any longer, and the shooting starts.

The director has fixed the order in which the gangsters pull their triggers. Gangster ii shoots at gangster pip_i at exactly the moment tit_i, unless gangster ii has already been killed by then. A gangster is killed at exactly the moment someone shoots at him.

The director wants to know how many gangsters are alive at the end of the scene. Byteberg is not yet sure about the order in which the gangsters shoot. From time to time he orders one of the values tit_i to be changed. After every such change he wants to know how many gangsters survive under the new order, taking into account all changes made so far.

Input

The first line contains one integer nn (2n200,0002 \le n \le 200{,}000), the number of gangsters in the scene. The second line contains nn integers p1,p2,,pnp_1, p_2, \dots, p_n (1pin1 \le p_i \le n, piip_i \ne i, and pipjp_i \ne p_j for iji \ne j), where gangster ii aims at gangster pip_i.

The third line contains nn integers u1,u2,,unu_1, u_2, \dots, u_n (1ui1091 \le u_i \le 10^9) describing the initial shooting order: the initial value of tit_i is uiu_i.

The fourth line contains one integer qq (0q200,0000 \le q \le 200{,}000), the number of changes to t1,,tnt_1, \dots, t_n that Byteberg plans. Each of the next qq lines describes one change. The ii-th of these lines contains two integers kik_i and viv_i (1kin1 \le k_i \le n, 1vi1091 \le v_i \le 10^9), meaning that the ii-th change sets tkit_{k_i} to viv_i. The numbers u1,u2,,un,v1,v2,,vqu_1, u_2, \dots, u_n, v_1, v_2, \dots, v_q are pairwise distinct.

Output

Print exactly q+1q+1 lines. The first line contains the number of gangsters who survive the shooting under the initial order. The ii-th of the following qq lines contains the number of gangsters who survive when the shooting order is given by t1,,tnt_1, \dots, t_n after the first ii changes have been applied.