#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iostream>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <complex>
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define pf(a) push_front((a))
#define rb() pop_back()
#define rf() pop_front()
#define sz(a) ((int)a.size())
using namespace std;
typedef long long lld;
typedef pair<int, int> pii;
typedef pair<lld, lld> pll;
typedef pair<lld, int> pli;
typedef pair<int, lld> pil;
typedef vector<vector<int>> Matrix;
typedef vector<vector<int>> Adj;
typedef vector<int> Row;
typedef complex<double> Complex;
typedef vector<Complex> Vcomplex;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const lld LINF = 1e18;
const double FINF = 1e15;
const double EPS = 1e-9;
const double PI = 2.0 * acos(0.0);
const int M = 19;
int n, m;
int c[505];
vector<int> g[505];
vector<int> g2[250005];
int par[250005][25];
int p[250005];
int cost[250005][25];
int depth[250005];
inline int encode(int i, int j) {
return i * n + j;
}
int max_cost(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
int ret = 0;
int d = depth[u] - depth[v];
for (int i = 0; i < M; ++i) {
if (d&1<<i) {
ret = max(ret, cost[u][i]);
u = par[u][i];
}
}
if (u == v) return ret;
for (int i = M - 1; i >= 0; --i) {
if (par[u][i] != par[v][i]) {
ret = max(ret, cost[u][i]);
ret = max(ret, cost[v][i]);
u = par[u][i];
v = par[v][i];
}
}
return max(ret, max(cost[u][0], cost[v][0]));
}
void dfs(int cur) {
for (auto nxt : g2[cur]) {
if (nxt != par[cur][0]) {
depth[nxt] = depth[cur] + 1;
par[nxt][0] = cur;
cost[nxt][0] = max(cost[nxt][0], c[cur/n] * c[cur%n]);
dfs(nxt);
}
}
}
int f(int x) {
return p[x] = (x == p[x] ? x : f(p[x]));
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; ++i) scanf("%d", &c[i]);
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
--u, --v;
g[u].pb(v);
g[v].pb(u);
}
vector<pii> V;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cost[encode(i, j)][0] = c[i] * c[j];
V.emplace_back(c[i] * c[j], encode(i, j));
}
}
for (int i = 0; i < n * n; ++i) p[i] = i;
sort(V.begin(), V.end());
for (pii vv : V) {
int u = vv.second / n;
int v = vv.second % n;
for (int nxt : g[u]) {
int en = encode(nxt, v);
if (c[nxt] * c[v] <= c[u] * c[v]) {
int cp = f(vv.second);
int np = f(en);
if (cp != np) {
p[cp] = np;
g2[vv.second].pb(en);
g2[en].pb(vv.second);
}
}
}
for (int nxt : g[v]) {
int en = encode(u, nxt);
if (c[u] * c[nxt] <= c[u] * c[v]) {
int cp = f(vv.second);
int np = f(en);
if (cp != np) {
p[cp] = np;
g2[vv.second].pb(en);
g2[en].pb(vv.second);
}
}
}
}
dfs(0);
for (int i = 0; i < M - 1; ++i) {
for (int j = 0; j < n * n; ++j) {
par[j][i+1] = par[par[j][i]][i];
cost[j][i+1] = max(cost[j][i], cost[par[j][i]][i]);
}
}
int q;
scanf("%d", &q);
for (int i = 0; i < q; ++i) {
int u, v;
scanf("%d %d", &u, &v);
--u, --v;
printf("%d\n", max_cost(encode(u, v), encode(v, u)));
}
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjYXNzZXJ0PgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxkZXF1ZT4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxjb21wbGV4PgoKI2RlZmluZSBtcChhLCBiKSB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2NhbmYoIiVkICVkIiwgJm4sICZtKTsKICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgc2NhbmYoIiVkIiwgJmNbaV0pOwogIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgKytpKSB7CiAgICBpbnQgdSwgdjsKICAgIHNjYW5mKCIlZCAlZCIsICZ1LCAmdik7CiAgICAtLXUsIC0tdjsKICAgIGdbdV0ucGIodik7CiAgICBnW3ZdLnBiKHUpOwogIH0KICB2ZWN0b3I8cGlpPiBWOwogIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICBmb3IgKGludCBqID0gMDsgaiA8IG47ICsraikgewogICAgICBjb3N0W2VuY29kZShpLCBqKV1bMF0gPSBjW2ldICogY1tqXTsKICAgICAgVi5lbXBsYWNlX2JhY2soY1tpXSAqIGNbal0sIGVuY29kZShpLCBqKSk7CiAgICB9CiAgfQogIGZvciAoaW50IGkgPSAwOyBpIDwgbiAqIG47ICsraSkgcFtpXSA9IGk7CiAgc29ydChWLmJlZ2luKCksIFYuZW5kKCkpOwogIGZvciAocGlpIHZ2IDogVikgewogICAgaW50IHUgPSB2di5zZWNvbmQgLyBuOwogICAgaW50IHYgPSB2di5zZWNvbmQgJSBuOwogICAgZm9yIChpbnQgbnh0IDogZ1t1XSkgewogICAgICBpbnQgZW4gPSBlbmNvZGUobnh0LCB2KTsKICAgICAgaWYgKGNbbnh0XSAqIGNbdl0gPD0gY1t1XSAqIGNbdl0pIHsKICAgICAgICBpbnQgY3AgPSBmKHZ2LnNlY29uZCk7CiAgICAgICAgaW50IG5wID0gZihlbik7CiAgICAgICAgaWYgKGNwICE9IG5wKSB7CiAgICAgICAgICBwW2NwXSA9IG5wOwogICAgICAgICAgZzJbdnYuc2Vjb25kXS5wYihlbik7CiAgICAgICAgICBnMltlbl0ucGIodnYuc2Vjb25kKTsKICAgICAgICB9CiAgICAgIH0KICAgIH0KICAgIGZvciAoaW50IG54dCA6IGdbdl0pIHsKICAgICAgaW50IGVuID0gZW5jb2RlKHUsIG54dCk7CiAgICAgIGlmIChjW3VdICogY1tueHRdIDw9IGNbdV0gKiBjW3ZdKSB7CiAgICAgICAgaW50IGNwID0gZih2di5zZWNvbmQpOwogICAgICAgIGludCBucCA9IGYoZW4pOwogICAgICAgIGlmIChjcCAhPSBucCkgewogICAgICAgICAgcFtjcF0gPSBucDsKICAgICAgICAgIGcyW3Z2LnNlY29uZF0ucGIoZW4pOwogICAgICAgICAgZzJbZW5dLnBiKHZ2LnNlY29uZCk7CiAgICAgICAgfQogICAgICB9CiAgICB9CiAgfQogIGRmcygwKTsKICBmb3IgKGludCBpID0gMDsgaSA8IE0gLSAxOyArK2kpIHsKICAgIGZvciAoaW50IGogPSAwOyBqIDwgbiAqIG47ICsraikgewogICAgICBwYXJbal1baSsxXSA9IHBhcltwYXJbal1baV1dW2ldOwogICAgICBjb3N0W2pdW2krMV0gPSBtYXgoY29zdFtqXVtpXSwgY29zdFtwYXJbal1baV1dW2ldKTsKICAgIH0KICB9CiAgaW50IHE7CiAgc2NhbmYoIiVkIiwgJnEpOwogIGZvciAoaW50IGkgPSAwOyBpIDwgcTsgKytpKSB7CiAgICBpbnQgdSwgdjsKICAgIHNjYW5mKCIlZCAlZCIsICZ1LCAmdik7CiAgICAtLXUsIC0tdjsKICAgIHByaW50ZigiJWRcbiIsIG1heF9jb3N0KGVuY29kZSh1LCB2KSwgZW5jb2RlKHYsIHUpKSk7CiAgfQp9