#include <bits/stdc++.h>
using namespace std;
#define what_is(x) cerr << #x << " is " << x << endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define st first
#define nd second
typedef long long ll;
typedef pair<int,int> pii;
const int N = 200+5;
const int INF = 1e9;
const int MOD = 1e9+7;
int n, m;
int a[N];
int main()
{
IOS
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc = 1;
while(cin >> n){
for(int i=1; i<=n; i++) cin >> a[i];
vector<tuple<int,int,int>> edges;
vector<int> g[n+2];
cin >> m;
while(m--){
int u, v; cin >> u >> v;
edges.push_back({u, v, pow(a[v]-a[u], 3)});
g[u].push_back(v);
}
vector<int> d(n+2, INF);
d[1] = 0; //source 1
for(int i=1; i<=n-1; i++){
for(auto e:edges){
int u, v, w; tie(u, v, w) = e;
// what_is(u); what_is(v); what_is(w);
if (d[u] == INF) continue;
d[v] = min(d[v], d[u]+w);
}
}
for(auto e:edges){
int u, v, w; tie(u, v, w) = e;
if (d[u] == INF) continue;
if (d[u] + w < d[v]) {
d[v] = -INF;
}
}
int q; cin >> q;
cout << "Set #" << tc++ << endl;
while(q--){
int des; cin >> des;
if (d[des] < 3 || d[des] == INF) cout << "?" << endl;
else cout << d[des] << endl;
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgd2hhdF9pcyh4KSBjZXJyIDw8ICN4IDw8ICIgaXMgIiA8PCB4IDw8IGVuZGw7CiNkZWZpbmUgSU9TIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsgY2luLnRpZSgwKTsKI2RlZmluZSBzdCBmaXJzdAojZGVmaW5lIG5kIHNlY29uZAoKdHlwZWRlZiBsb25nIGxvbmcgbGw7CnR5cGVkZWYgcGFpcjxpbnQsaW50PiBwaWk7CmNvbnN0IGludCBOID0gMjAwKzU7CmNvbnN0IGludCBJTkYgPSAxZTk7CmNvbnN0IGludCBNT0QgPS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