#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define all(x) x.begin(), x.end()
#define int long long
#define pq priority_queue
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pp pop_back
#define F first
#define S second
using namespace std;
vector<int> adj[100010];
int al = 0;
int bfs(int s, int e) {
	vector<int> vis(100010, -1);
	vis[s] = 0;
	pq<pair<int, int>> q;
	q.push({0, s});
	while (!q.empty()) {
		int d = q.top().F;
		s = q.top().S;
		q.pop();
		if (vis[s] > d) continue ;
		for (auto i: adj[s]) {
			if (i == s) continue ;
			if (vis[s] >= vis[i]) {
				vis[i] = vis[s] + 1;
				q.push({vis[i], i});
			}
		}
	}
	return vis[e];
}
void solve () {
	int n, m, q;
	cin >> n >> m >> q;
	for (int i = 1; i <= m; i++) {
		int a, b;
		cin >> a >> b;
		if (a > b) swap(a, b);
		adj[a].pb(b);
	}
	while (q--) {
		int t, y;
		cin >> t >> y;
		vector<int> c(n+1, 0);
		for (int i = 1; i <= y; i++) {
			int x; cin >> x;
			c[x] = 1;
		}
		int ans = -1;
		for (int i = 1; i <= t; i++) {
			if (c[i]) continue ;
			int d = bfs(i, t);
			ans = max(ans, d);
		}
		cout << ans << '\n';
	}
}
signed main() {IOS solve(); return 0;}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |