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 <cstdio>
#include <cstring>
#include <string>
#include <cassert>

using namespace std;

const int N = 100005;
int f[N],num[N];
struct node {
    int x,y,s;
};

node a[N];
pair<int,int> b[N];

int getf(int x) {
    return (f[x] == x)?x:(f[x] = getf(f[x]));
}

void make(int x,int y) {
    x = getf(x);
    y = getf(y);
    if (x == y) {
        return;
    }
    if (num[x] < num[y]) {
        f[x] = y;
        num[y] += num[x];
    }
    else {
        f[y] = x;
        num[x] += num[y];
    }
}

bool check(int m,int n,int p,int v) {
    for (int i = 0; i < n; ++i) {
        num[f[i] = i] = 1;
    }
    for (int i = 0; i < m; ++i) {
        if (a[i].s <= v) {
            make(a[i].x,a[i].y);
        }
    }
    for (int i = 0; i < p; ++i) {
        if (getf(b[i].first) != getf(b[i].second)) {
            return false;
        }
    }
    return true;
}

int main() {
int n,m;
    scanf("%d%d",&n,&m);
    assert((n >= 1) && (n <= 100000));
    assert((m >= 1) && (m <= 100000));
    int left = 2000000, right = 0;
    for (int i = 0; i < m; ++i) {
        scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].s);
        assert((a[i].x >= 1) && (a[i].x <= n));
        assert((a[i].y >= 1) && (a[i].y <= n) && (a[i].x != a[i].y));
        assert((a[i].s >= 1) && (a[i].s <= 1000000));
        --a[i].x;
        --a[i].y;
        left = min(left, a[i].s);
        right = max(right, a[i].s);
    }
    int p;
    scanf("%d",&p);
    assert((p >= 1) && (p <= 100000));
    for (int i = 0; i < p; ++i) {
        scanf("%d%d",&b[i].first,&b[i].second);
        assert((b[i].first >= 1) && (b[i].first <= n));
        assert((b[i].second >= 1) && (b[i].second <= n) && (b[i].first != b[i].second));
        --b[i].first;
        --b[i].second;
    }
    int answer = -1;
    while (left <= right) {
        int mid =(left + right) >> 1;
        if (check(m,n,p,mid)) {
            answer = mid;
            right = mid - 1;
        }
        else {
            left = mid + 1;
        }
    }
    if (answer >= 0) {
        printf("%d\n",answer);
    }
    else {
        puts("MISSION IMPOSSIBLE");
    }
    return 0;
}
```