Under construction

Still being built. Things may change

Road to the Olympiad

Time limit2sMemory limit1024 MB

Summary
Given a graph where flights are added and removed, find the cheapest route between two cities using at most two flights.
Level

Hard8 of 10

Topics
Graph, Hash map, Shortest path
Solved
No attempts yet

Statement

Miша solved every problem in the qualifying round and received an invitation to the on-site stage of a closed informatics olympiad. He plans to fly to the venue and wants to make at most one transfer, so he uses at most two flights. Among all such options, he wants the cheapest one.

The jury keeps the venue and date of the on-site stage secret, so Miша cannot know in advance which city he will start from. New regular flights are added from time to time, and some old ones are removed from the schedule. Write a program that answers the following queries:

  1. Add a new regular flight.
  2. Remove a flight.
  3. Find the minimum cost of a route between two cities, considering only routes with no transfers and routes with one transfer.

Each flight connects a pair of cities uiu_i and viv_i and has cost cic_i. All flights are two-way, so they can be used in both directions.

Input

The first line contains two integers nn and mm (2≤n≤100 0002 \leq n \leq 100\,000, 0≤m≤100 0000 \leq m \leq 100\,000), the number of cities and the number of flights available at the start.

Each of the next mm lines contains three integers uiu_i, viv_i, and cic_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i \ne v_i, 0≤ci≤1090 \leq c_i \leq 10^9), the cities connected by the ii-th flight and its cost.

The next line contains one integer qq (1≤q≤100 0001 \leq q \leq 100\,000), the number of queries.

Each of the next qq lines describes a query in one of three formats:

  • 1 ui vi ci1\ u_i\ v_i\ c_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i \ne v_i, 0≤ci≤1090 \leq c_i \leq 10^9) adds a regular flight between cities uiu_i and viv_i with cost cic_i.
  • 2 ui vi2\ u_i\ v_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i \ne v_i) removes the regular flight between cities uiu_i and viv_i. It is guaranteed that such a flight exists at the time of the query.
  • 3 ui vi3\ u_i\ v_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i \ne v_i) asks for the cheapest way to travel from city uiu_i to city viv_i, using at most one transfer.

At any moment, there is at most one regular flight between any pair of cities. There is at least one query of the third type.

Output

For each query of the third type, print the minimum cost of a trip using at most one transfer. If no suitable route exists between the given pair of cities, print −1-1.

Hint

For the sample queries of the third type, the optimal routes in input order are:

  1. Through city 2. A direct flight exists, but it is not the cheapest route.
  2. Through city 3.
  3. Through city 2.
  4. Through city 3.
  5. No suitable route exists.
  6. The direct flight.
  7. Through city 1.
  8. Through city 4.
  9. No suitable route exists.

Examples1

  1. Example 1

    Input
    5 6
    1 2 3
    1 3 8
    2 3 4
    2 4 7
    3 4 1
    4 5 7
    13
    3 1 3
    3 1 4
    2 1 3
    3 1 4
    1 1 3 0
    3 1 4
    3 1 5
    3 3 4
    1 1 4 1
    3 2 4
    3 2 5
    2 4 5
    3 2 5
    
    Expected output
    7
    9
    10
    1
    -1
    1
    4
    14
    -1