#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  const int64_t inf = LLONG_MAX / 2;
  vector<int64_t> a(n);
  vector<int> t(n);

  for (int i = 0; i < n; ++i) cin >> a[i] >> t[i];

  auto f = [&](int64_t x) {
    for (int i = 0; i < n; ++i) {
      if (t[i] == 1) x += a[i];
      else if (t[i] == 2) x = max(x, a[i]);
      else x = min(x, a[i]);
    }
    return x;
  };

  int64_t lb = -inf;
  int64_t ub = inf;
  int64_t d = 0;
  for (int i = 0; i < n; ++i) {
    if (t[i] == 1) lb += a[i], ub += a[i], d += a[i];
    else if (t[i] == 2) lb = max(lb, a[i]);
    else ub = min(ub, a[i]);
    if (lb > ub) {
      if (t[i] == 2) ub=lb;
      else lb=ub;
    }
  }
  lb -= d, ub -= d;
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int64_t x;
    cin >> x;
    if (x <= lb) cout << lb + d << endl;
    else if (x >= ub) cout << ub + d << endl;
    else cout << x + d << endl;
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 4;
vector<int> adj[N];
int c[20] , d[20][N];
int inf;
void bfs(int idx){
    int src = c[idx];
    queue<int> q;
    q.push(src);
    d[idx][src] = 0;
    while((int)q.size()){
        int tp = q.front();
        q.pop();
        for(int v : adj[tp]){
            if(d[idx][v] >= inf){
                d[idx][v] = d[idx][tp] + 1;
                q.push(v);
            }
        }
    }
}

int memo[18][(1<<18)+3] , k;

int solve(int idx , int msk){
    if(__builtin_popcount(msk) == k) {
        return 0;
    }
    int &ret = memo[idx][msk];
    if(~ret)
        return ret;
    ret = 2e7;
    for(int i = 0 ; i < k ; ++i){
        if((msk >> i) & 1)continue;
        ret = min(ret , d[idx][c[i]] + solve(i , msk | (1 << i)));
    }
    return ret;
}
void solve(){
    int n , m;
    cin >> n >> m;
    for(int i = 0 ; i < m ; ++i){
        int u , v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    memset(d , '?' , sizeof d);
    inf = d[0][0];
    cin >> k;
    for(int i = 0 ; i < k ; ++i){
        cin >> c[i];
        bfs(i);
    }
    memset(memo , -1 , sizeof(memo));
    int rs = 1 + solve(0 , 1);
    for(int i = 1 ; i < k ; ++i){
        rs = min(rs , 1 + solve(i , (1 << i)));
    }
    cout << (rs >= 2e7 ? -1 : rs) << '\n';
}
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    int t = 1;
//    cin >> t;
    while(t--){
        solve();
    }
}