This page is still under construction.

Parts of this page are still being built. What you see may change.

Switch Grass

Time limit2sMemory limit512 MB

Summary
A weighted connected graph has a color at each vertex; after each of Q point color updates, report the shortest distance between two vertices of different colors.
Level

Hard9 of 10

Topics
Graph, Shortest path, Divide and conquer, Dynamic programming
Solved
No attempts yet

Problem

Farmer John plants several kinds of grass on his farm, because different cows like different grass. Grass of different kinds has to be planted far enough apart. Planted close together, two kinds mix and cannot be separated again.

The farm has NN fields (1≤N≤200 0001 \le N \le 200\,000), and MM pairs of fields are joined by two-way pathways (1≤M≤200 0001 \le M \le 200\,000). Using only these pathways, you can walk from any field to any other field. Each pathway has an integer length between 11 and 1 000 0001\,000\,000, and at most one pathway joins any given pair of fields.

John plants one of KK kinds of grass in each field (1≤K≤N1 \le K \le N). Later he sometimes replaces the grass in one field with another kind. Call that an update. Several updates happen over time, and every update stays in effect.

After each update John wants the length of the shortest path between two fields whose grass kinds differ. That is, among all pairs of fields with different kinds, the distance of the closest pair. A large value means one kind is unlikely to mix with another. After every update the farm always has at least two fields with different kinds of grass.

In 30 percent of the inputs, each field is directly joined to at most 10 pathways.

Input

The first line contains four integers NN, MM, KK, and QQ, where QQ is the number of updates (1≤Q≤200 0001 \le Q \le 200\,000).

Each of the next MM lines describes one pathway with three integers AA, BB, and LL, meaning a pathway of length LL between field AA and field BB (1≤A,B≤N1 \le A, B \le N, 1≤L≤1 000 0001 \le L \le 1\,000\,000).

The next line contains NN integers, the kind of grass first planted in fields 11 through NN in order. Each value is between 11 and KK.

Each of the last QQ lines describes one update with two integers AA and BB, meaning the grass in field AA becomes kind BB (1≤A≤N1 \le A \le N, 1≤B≤K1 \le B \le K).

Output

For each update, print on its own line the length of the shortest path between two fields with different kinds of grass, after the update is applied.

Examples1

  1. Example 1

    Input
    3 2 3 4
    1 2 3
    2 3 1
    1 1 2
    3 3
    2 3
    1 2
    2 2
    
    Expected output
    1
    3
    3
    1