Under construction

Still being built. Things may change

Link Cut Tomato

Time limit3.5sMemory limit1024 MB

Summary
Given a graph whose edges form or break on given days, find the earliest day each tomato ripens when ripeness spreads across connected edges.
Level

Hard8 of 10

Topics
Graph, Union-find, Segment tree, Shortest path
Solved
No attempts yet

Problem

It is well known that tomatoes form a network by connecting and disconnecting with each other, using their stems like antennas. A connection between two tomatoes can form or break only at the moment the date changes, and the connection state of any two tomatoes does not change more than once a day.

John, a farmer who majored in tomato networks, found a link between the connection states of tomatoes and how ripe they are. According to John's finding, if a ripe tomato and an unripe tomato are connected for a day, the unripe tomato ripens under the influence of the ripe one.

After long research, John built a device that observes the connection states between tomatoes. With this device, you can see which tomatoes are ripe on day 00 and which tomatoes are connected on day 00. You can also track when a connection between any two tomatoes forms or breaks.

Chanwoo spent all the money he had for setting problems for the Cheonha Jeil Coding Contest on John's device and NN tomatoes numbered 11 through NN, but he could not figure out when each tomato ripens. Write a program that computes the ripening day of each tomato from the information obtained from John's device, so that Chanwoo can eat the ripe tomatoes.

Input

The first line contains four integers NN, MM, KK, and QQ, separated by spaces, which are the number of tomatoes, the number of connected pairs on day 00, the number of tomatoes that are ripe on day 00, and the number of changes in connection states. (1≤K≤N≤200 000;(1 \leq K \leq N \leq 200\,000; 0≤M,Q≤200 000)0 \leq M, Q \leq 200\,000)

The next MM lines contain the initial connection states on day 00, with no duplicates. Each line contains two tomato numbers aa and bb, separated by a space, that are connected on day 00. (1≤a<b≤N)(1 \leq a \lt b \leq N)

The next line contains the numbers X1,X2,⋯ ,XKX_1, X_2, \dotsm, X_K of the distinct tomatoes that are ripe on day 00, separated by spaces. (1≤Xi≤N)(1 \leq X_i \leq N)

The next QQ lines describe the changes in connection states in the order they occur. Each line contains a day TT and two tomato numbers xx and yy, separated by spaces. If the two tomatoes were connected on day T−1T-1, they become disconnected from midnight of day TT. If they were not connected on day T−1T-1, they become newly connected from midnight of day TT. Every pair (T,x,y)(T, x, y) in the input is distinct. (1≤T≤200 000;(1 \leq T \leq 200\,000; 1≤x<y≤N)1 \leq x \lt y \leq N)

All numbers in the input are integers.

Output

On the first line, print NN integers separated by spaces. For the ii-th tomato, print −1-1 if it never ripens, or the day it ripens otherwise.

Hint

The input and output are large, so it is recommended to use the fast I/O described in the language guide on pages 2-4 of the problem set.

Examples3

  1. Example 1

    Input
    10 11 2 8
    1 2
    6 7
    2 3
    3 4
    4 6
    1 3
    4 5
    3 6
    8 9
    6 8
    4 10
    1 5
    1 1 2
    1 4 10
    2 6 8
    2 2 9
    2 6 7
    3 6 8
    3 7 9
    3 8 9
    
    Expected output
    0 1 1 1 0 2 4 4 3 -1
    
  2. Example 2

    Input
    6 5 1 0
    1 2
    3 4
    1 4
    5 6
    2 3
    1
    
    Expected output
    0 1 2 1 -1 -1
    
  3. Example 3

    Input
    5 0 2 4
    1 2
    1 1 5
    1 3 4
    3 2 3
    3 4 5
    
    Expected output
    0 0 4 4 2