#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <numeric>
#include <chrono>
#include <random>
#include <bitset>
#include <tuple>
#include <queue>
#include <map>
#include <unordered_map>
#include <cstring>
#include <cassert>
#include <climits>
#include <math.h>
using namespace std;

const int N = 2e5 + 10, mod = 1e9 + 7;
int n, q, a[N], t[N];
long long pref[N];
map<long long, int> mxid, mnid;
set<long long> mns, mxs;

struct tr {
    int tree[N << 1];
    void build() {
        for(int i = 0; i < n; i++) tree[i + n] = 1e9;
        for(int i = n - 1; i > 0; i--) tree[i] = min(tree[i << 1], tree[i << 1 | 1]);
    }

    void upd(int i, int va) {
        tree[i += n] = va;
        for(i >>= 1; i > 0; i >>= 1) tree[i] = min(tree[i << 1], tree[i << 1 | 1]);
    }

    int query(int l, int r) {
        int resl = 1e9, resr = 1e9;
        for(l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
            if(l & 1) resl = min(resl, tree[l++]);
            if(r & 1) resr = min(resr, tree[--r]);
        }
        return min(resl, resr);
    }
} mntr, mxtr;

int main() {
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1;
    //scanf("%d", &tc);
    for(int cn = 1; cn <= tc; cn++) {
        scanf("%d", &n);
        for(int i = 0; i < n; i++) scanf("%d%d", &a[i], &t[i]);
        for(int i = 0; i < n; i++) {
            if(i) pref[i] = pref[i - 1];

            if(t[i] == 1) pref[i] += a[i];
            else if(t[i] == 2) mxid[a[i] - pref[i]];
            else mnid[a[i] - pref[i]];
        }
        mntr.build();
        mxtr.build();
        int c = 0;
        for(auto &x : mxid) {
            x.second = c++;
            mxs.insert(x.first);
        }
        c = 0;
        for(auto &x : mnid) {
            x.second = c++;
            mns.insert(x.first);
        }

        vector<long long> ans(n);
        for(int i = n - 1; i >= 0; i--) {
            if(t[i] == 1) continue;

            auto it = mxs.lower_bound(a[i] - pref[i]);
            int j = 1e9;
            if(it != mxs.end()) {
                j = mxtr.query(mxid[*it], n - 1);
            }

            it = mns.upper_bound(a[i] - pref[i]);
            int k = 1e9;
            if(it != mns.begin()) {
                it--;
                k = mntr.query(0, mnid[*it]);
            }

            if(min(j, k) < n) ans[i] = ans[min(j, k)];
            else ans[i] = 1ll * a[i] + pref[n - 1] - pref[i];

            if(t[i] == 2) mxtr.upd(mxid[a[i] - pref[i]], i);
            else mntr.upd(mnid[a[i] - pref[i]], i);
        }

        scanf("%d", &q);
        while(q--) {
            long long x; scanf("%lld", &x);
            
            auto it = mxs.lower_bound(x);
            int j = 1e9;
            if(it != mxs.end()) {
                j = mxtr.query(mxid[*it], n - 1);
            }

            it = mns.upper_bound(x);
            int k = 1e9;
            if(it != mns.begin()) {
                it--;
                k = mntr.query(0, mnid[*it]);
            }

            if(min(j, k) < n) printf("%lld\n", ans[min(j, k)]);
            else printf("%lld\n", x + pref[n - 1]);
        }
    }
    return 0;
}

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

const int N = 17;
int n,m,k;
vector<vector<int>> G;
int d[N][N], c[N];
int dp[1 << N][N];

void bfs(int s) {
  vector<int> dist(n, INT_MAX);
  queue<int> que;
  que.push(c[s]);
  dist[c[s]] = 0;
  while(!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto next : G[v]) {
      if (dist[next] != INT_MAX) continue;
      dist[next] = dist[v] + 1;
      que.push(next);
    }
  }
  for (int i = 0; i < k; i++) {
    d[s][i] = min(d[s][i], dist[c[i]]);
    d[i][s] = min(d[i][s], dist[c[i]]);
  }
}

signed main() {
  cin >> n >> m;

  G.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b; cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = INT_MAX;
    }
  }

  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> c[i], c[i]--;
  }
  for (int i = 0; i < k; i++) {
    bfs(i);
  }

  for (int i = 0; i < (1 << N); i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = INT_MAX;
    }
  }

  for (int i = 0; i < k; i++) {
    dp[(1 << k) - 1][i] = 0;
  }

  // d[N][N]はidxで管理 (idx < k)
  for (int s = (1 << k) - 2; s >= 0; s--) {
    for (int v = 0; v < k; v++) {
      for (int u = 0; u < k; u++) {
        if (!(s >> u & 1)) {
          dp[s][v] = min(dp[s][v], dp[s | 1 << u][u] + d[v][u]);
        }
      }
    }
  }

  int res = INT_MAX;
  for (int i = 0; i < k; i++) {
    res = min(res, dp[0][i]);
  }

  if (res == INT_MAX) res = -1;
  else res += 1;
  cout << res << '\n';
}




