Jim and his LAN Party
Difficulty: Hard

During the Steam Summer Sale, Jim's $N-1$ friends have purchased $M$ games, which are numbered from $1$ to $M$. The games are multiplayer. Jim has invited his friends to his basement where they will play by making a LAN-Party. 

Each friend has already decided the game he would like to play for the rest of the day. So there will be a group of friends who will play the same game together.

But then, they face a problem: Currently, none of the friends' PCs are connected. So they have to be connected using the available $Q$ wires. Jim decides to connect friends $u_i$ and $v_i$ with the $i$<sup>th</sup> wire one by one. So he starts with wire 1, then with wire 2 and so on. 

A group can start playing their game, only if all the members are connected (if not directly, then there must exist a path connecting them). They want to start playing as soon as possible. 

For each game, find out the wire after adding which the group can start playing. It is also possible that a group will never get connected. In such a case, this group starts crying and you should display `-1`.


Input Format:
On the first line there will be $N$, $M$ and $Q$ each separated by a single space. On the second line we will give you $N$ integers separated by a single space: The $i$-th integer denotes the game friend $i$ wants to play (all between $1$ and $M$). The next $Q$ lines will denote $Q$ wires: i<sup>th</sup> line denotes i<sup>th</sup> wire and is denoted by $u_i$ and $v_i$ pairs each separated by a single space. 


Output Format:
Print on the $i$<sup>th</sup> line the answer for the $i$<sup>th</sup> game. 


Sample Input:
    5 2 4
    1 2 2 2 1
    1 2 
    2 3
    1 5
	4 5	


Sample Output:
	3
	4


Python 3 starter code:
#
# Complete the 'lanParty' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
#  1. INTEGER_ARRAY games
#  2. 2D_INTEGER_ARRAY wires
#  3. INTEGER m
#

def lanParty(games, wires, m):
    # Write your code here




Reference solution (C++):
###C++  
```cpp  
#include <iostream>
#include <vector>
using namespace std;
#define SZ(x) ( (int) (x).size() )
const int MAX_N = 100000 + 1;
int N, M, Q;
int color[MAX_N];
vector<int> cpos[MAX_N];
vector<int> q[MAX_N];
int qu[MAX_N], qv[MAX_N];
int lo[MAX_N], hi[MAX_N];
// implementation of UFDS
int f[MAX_N];
int getf(int x){
  return f[x] == x ? x : f[x] = getf(f[x]);
}
void mergef(int x, int y){
  f[getf(x)] = getf(y);
}
void initf(){
  for(int i = 1; i <= N; i++){
    f[i] = i;
  }
}

void reloadQueries(){
  for(int i = 0; i <= Q; i++){
    q[i].clear();
  }
  for(int i = 1; i <= M; i++){
    q[(lo[i] + hi[i]) / 2].push_back(i);
  }
}

void answerQuery(int c){
  bool connected = true;
  for(int i = 1; i < (int) cpos[c].size(); i++){
    connected &= getf(cpos[c][i]) == getf(cpos[c][i - 1]);
  }
  int mid = (lo[c] + hi[c]) / 2;
  if(!connected){
    lo[c] = mid + 1;
  } else {
    hi[c] = mid;
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin >> N >> M >> Q;
  for(int i = 1; i <= N; i++){
    cin >> color[i];
    cpos[color[i]].push_back(i);
  }
  for(int i = 1; i <= M; i++){
    lo[i] = 0, hi[i] = Q;
  }
  for(int i = 1; i <= Q; i++){
    cin >> qu[i] >> qv[i];
  }
  for(int times = 25; times >= 0; times --){
    initf();
    reloadQueries();
    for(int i = 0; i <= Q; i++){
      if(i > 0)
    mergef(qu[i], qv[i]);
      for(int j = 0; j < SZ(q[i]); j++){
    answerQuery(q[i][j]);
      }
    }
  }
  for(int i = 1; i <= M; i++){
    if(lo[i] > Q){
      cout << -1 << '\n';
    } else {
      cout << lo[i] << '\n';
    }
  }
  return 0;
}
```