Switch Grass

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.

Hard9GraphShortest pathDivide and conquerDynamic programmingNo attempts yetTime limit2sMemory limit512 MB

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 (1N2000001 \le N \le 200\,000), and MM pairs of fields are joined by two-way pathways (1M2000001 \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 10000001\,000\,000, and at most one pathway joins any given pair of fields.

John plants one of KK kinds of grass in each field (1KN1 \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 (1Q2000001 \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 (1A,BN1 \le A, B \le N, 1L10000001 \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 (1AN1 \le A \le N, 1BK1 \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.