Under construction

Still being built. Things may change

Two Roads

Interview

Time limit3sMemory limit1024 MB

Summary
Given a directed graph, for each of Q scenarios that add two one-way roads, find the shortest time from home S to office T, or -1 if unreachable.
Level

Medium5 of 10

Topics
Shortest path, Graph
Solved
No attempts yet

Problem

Ulsan has NN intersections numbered from 11 to NN, and MM roads connecting them. Each road is one-way, and the time needed to travel along it is fixed. Yoon drives every day from home to the office in a car with Hyundai Mobis's self-driving system. Yoon's home is at intersection SS, and the office is at intersection TT. The self-driving system always uses a route that takes the shortest time from home to the office. Yoon recently heard that two new roads will be built in Ulsan. Yoon expects to reach the office faster, but Yoon does not know which intersections the new roads connect or how long they take. So Yoon assumes QQ road construction scenarios and wants to calculate the route the car would take in each case. For each scenario, find the time Yoon needs to drive from home to the office using the self-driving system.

Input

The first line contains the number of intersections NN, the number of existing roads MM, the home intersection SS, and the office intersection TT, separated by spaces. (2≤N≤3002\leq N\leq 300; 0≤M≤3 0000\leq M\leq 3\ 000; 1≤S,T≤N1\leq S,T\leq N; S≠TS\neq T)

The next MM lines each contain three integers uu, vv, ww separated by spaces. They describe a one-way road from intersection uu to intersection vv that takes ww time to traverse. (1≤u,v≤N1\leq u,v\leq N; 1≤w≤1061\leq w\leq 10^6)

The next line contains the number of construction scenarios QQ. (1≤Q≤100 0001\leq Q\leq 100\ 000)

The next QQ lines each contain six integers a1a_1, b1b_1, c1c_1, a2a_2, b2b_2, c2c_2 separated by spaces. They describe a scenario in which two new one-way roads are built: one from intersection a1a_1 to b1b_1 taking c1c_1 time, and one from intersection a2a_2 to b2b_2 taking c2c_2 time. (1≤a1,b1,a2,b2≤N1\leq a_1,b_1,a_2,b_2\leq N; 1≤c1,c2≤1061\leq c_1,c_2\leq 10^6)

The same pair of intersections may be connected by several roads, and a road may start and end at the same intersection.

Output

For each construction scenario, print on its own line the time Yoon needs to drive from home to the office. If the trip is impossible in a scenario, print −1-1 on that line.

Examples1

  1. Example 1

    Input
    5 4 1 5
    1 2 3
    2 3 3
    1 3 5
    3 5 2
    4
    1 4 2 4 5 3
    1 4 5 4 5 4
    2 5 3 3 5 1
    3 1 1 5 3 1
    
    Expected output
    5
    7
    6
    7