World Peace
Difficulty: Hard

The Artifact of World Peace was obtained by the evil Pharaoh. He smashed it into pieces and hid them inside a temple. The temple has a very elaborate and confusing floor plan. It contained different rooms. Each room has a trap. Moreover, to go from one room to another, you must pass by a corridor and each of these also has a trap. The Pharaoh suddenly realized he was low on budget because the college tuition fee of his children suddenly increased. Unprepared to sacrifice the future of his children, he decided to not push through with the traps inside the rooms BUT retain the traps in the corridors. Don't be fooled though, it is still very difficult to infiltrate this temple.

You decide that you want to get the pieces of this Sacred Artifact of _World Peace_, put these together to recreate the Artifact of _World Peace_ and then finally obtain millions of dollars by selling it on eBay.

You list down all the known rooms of this temple. Each corridor has a trap and a certain skill level is required to clear this trap. Furthermore, to secure all pieces of the artifact, you realize that you just need to form the safest routes between pairs of rooms.

To summarize, the temple has $R$ known rooms and $C$ corridors. Each corridor connects two rooms and has its corresponding trap. Each trap has a certain _skill level_ $s$ required in order to clear it. If your skill level is below this $s$, you fail the trap and subsequently die a slow, horrible and painful death. After planning, you realize you just have to travel between $P$ pairs of rooms. For each pair of rooms $A$ and $B$, you must select the safest corridors (i.e. the path which requires the lowest skill level) to travel from room $A$ to room $B$. Given these information, you must find out the minimum skill level you must have in order to survive travelling through the corridors which connect each of these pairs of rooms.

**Input Format**  
The first line of input contains two integers, the number of rooms $R$ in the temple and the number of corridors $C$ in the temple. The rooms are numbered from $1$ to $R$.  

The next $C$ lines each contain three integers: $A$, $B$ and $s$ in that order. This tells us that to survive the corridor connecting room $A$ and room $B$, your skill level must be $s$ or higher.  

The next line contains a single integer, $P$. This indicates how many pairs of rooms you must plan a route for.  

The next $P$ lines each contain two integers, $A$ and $B$. This tells us that you must plan the safest route between rooms $A$ and $B$.

**Output Format**  
Output a single line containing a single integer, $S$, the minimum skill level you must have in order to travel between each of the $P$ pairs of rooms.

If it is impossible to travel between one of the $P$ pairs of rooms, output `MISSION IMPOSSIBLE`.

**Constraints**  
$1 \le R \le 10^5$  
$1 \le C \le 10^5$  
$1 \le P \le 10^5$  
$1 \le A, B \le R$  
$A \ne B$  
$1 \le s \le 10^6$

**Sample Input 1**  

    8 12
    1 2 1
    1 4 2
    2 3 5
    2 4 4
    3 4 2
    5 2 11
    5 2 10
    5 6 1
    6 8 3
    7 6 2
    8 7 3
    5 8 4
    2
    1 3
    5 8

**Sample Output 1**  

    3

**Sample Input 2**  

    3 1
    1 3 100
    1
    1 2

**Sample Output 2**  

    MISSION IMPOSSIBLE

**Explanation**  
The first case corresponds to the following floor plan:  
![image](https://hr-challenge-images.s3.amazonaws.com/3129/3129.png)  


The safest path to travel from room $1$ to room $3$ is going from room $1$ to room $4$ then room $4$ to room $3$. The minimum skill level to safely traverse the corridors connecting these rooms is $2$. The safest path to travel from room $5$ to room $8$ is going from room $5$ to room $6$, then room $7$ and finally room $8$ (or if you're feeling less adventurous, you could skip going to room $7$ and travel from $5$ to $6$ to $8$ instead). The minimum skill level to safely traverse these corridors is $3$. Thus to travel between all $P = 2$ pairs of rooms, one must have a minimum skill level of $3$.

For the second case, there is no way to get from room $1$ to room $2$ regardless of your skill level. How did they get the pieces there? It shall remain a mystery.


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
```cpp
#include <stdio.h>

struct edge {
    int a, b, s;
};

int ea[111111];
int eb[111111];
int es[111111];
int pa[111111];
int pb[111111];
int ps[111111];
int r, c, p;

int find(int n) {
    return ps[n] < 0 ? n : ps[n] = find(ps[n]);
}

int kaya(int cost) {
    for (int i = 0; i < r; i++) ps[i] = -1;

    for (int i = 0; i < c; i++) {
        if (es[i] > cost) continue;
        int fa = find(ea[i]);
        int fb = find(eb[i]);
        if (fa != fb) {
            if (ps[fa] == ps[fb]) ps[fb]--;
            if (ps[fa] > ps[fb]) {
                ps[fa] = fb;
            } else {
                ps[fb] = fa;
            }
        }
    }

    for (int i = 0; i < p; i++) {
        if (find(pa[i]) != find(pb[i])) return 0;
    }
    return 1;
}

int main() {
    scanf("%d%d", &r, &c);
    for( int i = 0; i < c; i++) {
        int a, b, s;
        scanf("%d%d%d", &a, &b, &s);
        ea[i] = --a;
        eb[i] = --b;
        es[i] = s;
    }

    scanf("%d", &p);
    for (int i = 0; i < p; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        pa[i] = --a;
        pb[i] = --b;
    }

    int L = -1;
    int R = 1000001;

    if (!kaya(R)) {
        printf("MISSION IMPOSSIBLE\n");
    } else {
        while (R - L > 1) {
            int M = L + R >> 1;
            (kaya(M) ? R : L) = M;
        }
        printf("%d\n", R);
    }
}
```