# 864F(tarjan思想)

F. Cities Excursions
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n cities in Berland. Some pairs of them are connected with m directed roads. One can use only these roads to move from one city to another. There are no roads that connect a city to itself. For each pair of cities (x, y) there is at most one road from x to y.

A path from city s to city t is a sequence of cities p1p2, ... , pk, where p1 = spk = t, and there is a road from city pi to city pi + 1 for each i from 1 to k - 1. The path can pass multiple times through each city except t. It can't pass through t more than once.

A path p from s to t is ideal if it is the lexicographically minimal such path. In other words, p is ideal path from s to t if for any other path qfrom s to t pi < qi, where i is the minimum integer such that pi ≠ qi.

There is a tourist agency in the country that offers q unusual excursions: the j-th excursion starts at city sj and ends in city tj.

For each pair sjtj help the agency to study the ideal path from sj to tj. Note that it is possible that there is no ideal path from sj to tj. This is possible due to two reasons:

• there is no path from sj to tj;
• there are paths from sj to tj, but for every such path p there is another path q from sj to tj, such that pi > qi, where i is the minimum integer for which pi ≠ qi.

The agency would like to know for the ideal path from sj to tj the kj-th city in that path (on the way from sj to tj).

For each triple sjtjkj (1 ≤ j ≤ q) find if there is an ideal path from sj to tj and print the kj-th city in that path, if there is any.

Input

The first line contains three integers nm and q (2 ≤ n ≤ 3000,0 ≤ m ≤ 30001 ≤ q ≤ 4·105) — the number of cities, the number of roads and the number of excursions.

Each of the next m lines contains two integers xi and yi (1 ≤ xi, yi ≤ nxi ≠ yi), denoting that the i-th road goes from city xi to city yi. All roads are one-directional. There can't be more than one road in each direction between two cities.

Each of the next q lines contains three integers sjtj and kj (1 ≤ sj, tj ≤ nsj ≠ tj1 ≤ kj ≤ 3000).

Output

In the j-th line print the city that is the kj-th in the ideal path from sj to tj. If there is no ideal path from sj to tj, or the integer kj is greater than the length of this path, print the string '-1' (without quotes) in the j-th line.

Example
input
7 7 5
1 2
2 3
1 3
3 4
4 5
5 3
4 6
1 4 2
2 6 1
1 7 3
1 3 2
1 3 5

output
2
-1
-1
2
-1


#include <bits/stdc++.h>
using namespace std;
const int maxn = 3000 + 10;
int inf = 0x3f3f3f3f;
int n, m, q;
int ans[400010];
int num[maxn];
int Instack[maxn];
int dfn[maxn];
int low[maxn];
int tot;
vector<int> edge[maxn];
struct query{
int s, t, k, id;
}Query[400010];
vector<int> temp[maxn];
vector<int> S[maxn];
void init()
{
for(int i = 1; i <= n; i++)
{
edge[i].clear();
S[i].clear();
}
memset(ans, -1, sizeof(ans));

}
void dfs(int u, int status, int depth)
{
dfn[u] = ++tot;
low[u] = inf;
Instack[u] = 1;
num[depth] = u;
if(status)
{
for(int i = 0; i < temp[u].size(); i++)
{
int loc = temp[u][i];
int id = Query[loc].id;
int kk = Query[loc].k;
if(kk <= depth) ans[id] = num[kk];
}
}
for(int i = 0; i < edge[u].size(); i++)
{
int v = edge[u][i];
if(dfn[v] == 0)
{
dfs(v, status && dfn[u] < low[u], depth + 1);
low[u] = min(low[u], low[v]);
}
else if(Instack[v] == 1)
{
low[u] = min(low[u], dfn[v]);
}
}
Instack[u] = 0;
}
int main()
{
while(~scanf("%d%d%d", &n, &m, &q))
{
init();
int u, v;
for(int i = 1; i <= m; i++)
{
scanf("%d%d", &u, &v);
edge[u].push_back(v);
}
for(int i = 1; i <= n; i++)
{
sort(edge[i].begin(), edge[i].end());
}
int s, t, k;
for(int i = 1; i <= q; i++)
{
scanf("%d%d%d", &s, &t, &k);
Query[i].s = s;
Query[i].t = t;
Query[i].k = k;
Query[i].id = i;
S[s].push_back(i);
}
for(int i = 1; i <= n; i++)
{
memset(dfn, 0, sizeof(dfn));
memset(Instack, 0, sizeof(Instack));
for(int j = 1; j <= n; j++) temp[j].clear();
for(int j = 0; j < S[i].size(); j++)
{
int loc = S[i][j];
int tt = Query[loc].t;
temp[tt].push_back(loc);
}
tot = 0;
dfs(i, 1, 1);
}
for(int i = 1; i <= q; i++) printf("%d\n", ans[i]);
}
return 0;
}