#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define endl "\n"
ll gcd(ll a, ll b)
{
if(b==0)
{
return a;
}
return gcd(b, a%b);
}
const ll L = 505;
vector <ll> t(L);
vector <ll> f[L];
vector <ll> s[L];
bool visited[L];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll tc = 0;
while(true)
{
tc++;
ll v, e;
cin >> v >> e;
ll tot = 0;
if(!v && !e)
{
break;
}
for(ll i=0; i<L; i++)
{
f[i].clear();
s[i].clear();
t[i] = 0;
}
for(ll i=1; i<=v; i++)
{
cin >> t[i];
tot+=t[i];
}
for(ll i=0; i<e; i++)
{
ll a, b;
cin >> a >> b;
f[a].pb(b);
s[b].pb(a);
}
ll q;
cin >> q;
cout << "Case #" << tc << ":" << endl;
while(q--)
{
ll x;
cin >> x;
queue <ll> q1, q2;
for(ll i=0; i<L; i++)
{
visited[i] = false;
}
q1.push(x);
q2.push(x);
ll sum1 = 0, sum2 = 0;
while(!q1.empty())
{
ll top = q1.front();
q1.pop();
sum1+=t[top];
visited[top] = true;
for(auto i: f[top])
{
if(!visited[i])
{
q1.push(i);
}
}
}
while(!q2.empty())
{
ll top = q2.front();
q2.pop();
sum2+=t[top];
visited[top] = true;
for(auto i: s[top])
{
if(!visited[i])
{
q2.push(i);
}
}
}
cout << tot - sum1 - sum2 + t[x] << endl;
}
cout << endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiBsb25nIGxvbmcgaW50IGxsOwp0eXBlZGVmIHVuc2lnbmVkIGxvbmcgbG9uZyBpbnQgdWxsOwp0eXBlZGVmIGxvbmcgZG91YmxlIGxkOwp0eXBlZGVmIHBhaXIgPGxsLCBsbD4gcGxsOwp0eXBlZGVmIHBhaXIgPGludCwgaW50PiBwaWk7CgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIGZmIGZpcnN0CiNkZWZpbmUgc3Mgc2Vjb25kCiNkZWZpbmUgYWxsKGEpIGEuYmVnaW4oKSwgYS5lbmQoKQojZGVmaW5lIHN6KGEpIChsbCkoYS5zaXplKCkpCiNkZWZpbmUgZW5kbCAiXG4iCgpsbCBnY2QobGwgYSwgbGwgYikKewogICAgaWYoYj09MCkKICAgIHsKICAgICAgICByZXR1cm4gYTsKICAgIH0KCiAgICByZXR1cm4gZ2NkKGIsIGElYik7Cn0KCmNvbnN0IGxsIEwgPSA1MDU7Cgp2ZWN0b3IgPGxsPiB0KEwpOwp2ZWN0b3IgPGxsPiBmW0xdOwp2ZWN0b3IgPGxsPiBzW0xdOwpib29sIHZpc2l0ZWRbTF07CgppbnQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShOVUxMKTsgY291dC50aWUoTlVMTCk7CgogICAgbGwgdGMgPSAwOwogICAgCiAgICB3aGlsZSh0cnVlKQogICAgewogICAgICAgIHRjKys7CgogICAgICAgIGxsIHYsIGU7CiAgICAgICAgY2luID4+IHYgPj4gZTsKCiAgICAgICAgbGwgdG90ID0gMDsKCiAgICAgICAgaWYoIXYgJiYgIWUpCiAgICAgICAgewogICAgICAgICAgICBicmVhazsKICAgICAgICB9CgogICAgICAgIGZvcihsbCBpPTA7IGk8TDsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgZltpXS5jbGVhcigpOwogICAgICAgICAgICBzW2ldLmNsZWFyKCk7CiAgICAgICAgICAgIHRbaV0gPSAwOwogICAgICAgIH0KCgogICAgICAgIGZvcihsbCBpPTE7IGk8PXY7IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIGNpbiA+PiB0W2ldOwoKICAgICAgICAgICAgdG90Kz10W2ldOwogICAgICAgIH0KCiAgICAgICAgZm9yKGxsIGk9MDsgaTxlOyBpKyspCiAgICAgICAgewogICAgICAgICAgICBsbCBhLCBiOwogICAgICAgICAgICBjaW4gPj4gYSA+PiBiOwoKICAgICAgICAgICAgZlthXS5wYihiKTsKICAgICAgICAgICAgc1tiXS5wYihhKTsKICAgICAgICB9CgogICAgICAgIGxsIHE7CiAgICAgICAgY2luID4+IHE7CgogICAgICAgIGNvdXQgPDwgIkNhc2UgIyIgPDwgdGMgPDwgIjoiIDw8IGVuZGw7CgogICAgICAgIHdoaWxlKHEtLSkKICAgICAgICB7CiAgICAgICAgICAgIGxsIHg7CiAgICAgICAgICAgIGNpbiA+PiB4OwoKICAgICAgICAgICAgcXVldWUgPGxsPiBxMSwgcTI7CiAgICAgICAgICAgIAogICAgICAgICAgICBmb3IobGwgaT0wOyBpPEw7IGkrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgdmlzaXRlZFtpXSA9IGZhbHNlOwogICAgICAgICAgICB9CgogICAgICAgICAgICBxMS5wdXNoKHgpOwogICAgICAgICAgICBxMi5wdXNoKHgpOwoKICAgICAgICAgICAgbGwgc3VtMSA9IDAsIHN1bTIgPSAwOwoKICAgICAgICAgICAgd2hpbGUoIXExLmVtcHR5KCkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGxsIHRvcCA9IHExLmZyb250KCk7CiAgICAgICAgICAgICAgICBxMS5wb3AoKTsKCiAgICAgICAgICAgICAgICBzdW0xKz10W3RvcF07CgogICAgICAgICAgICAgICAgdmlzaXRlZFt0b3BdID0gdHJ1ZTsKCiAgICAgICAgICAgICAgICBmb3IoYXV0byBpOiBmW3RvcF0pCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaWYoIXZpc2l0ZWRbaV0pCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBxMS5wdXNoKGkpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQoKICAgICAgICAgICAgd2hpbGUoIXEyLmVtcHR5KCkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGxsIHRvcCA9IHEyLmZyb250KCk7CiAgICAgICAgICAgICAgICBxMi5wb3AoKTsKCiAgICAgICAgICAgICAgICBzdW0yKz10W3RvcF07CgogICAgICAgICAgICAgICAgdmlzaXRlZFt0b3BdID0gdHJ1ZTsKCiAgICAgICAgICAgICAgICBmb3IoYXV0byBpOiBzW3RvcF0pCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaWYoIXZpc2l0ZWRbaV0pCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBxMi5wdXNoKGkpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgY291dCA8PCB0b3QgLSBzdW0xIC0gc3VtMiArIHRbeF0gPDwgZW5kbDsKICAgICAgICB9CgogICAgICAgIGNvdXQgPDwgZW5kbDsKICAgIH0KCiAgICByZXR1cm4gMDsKfQ==