#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> using min_heap=priority_queue<T,vector<T>,greater<T>>;
template<typename T> using max_heap=priority_queue<T>;
template<typename T> T isqrt(const T &x){T y=sqrt(x+2); while(y*y>x) y--; return y;}
const int INF = 0x7f7f7f7f7f7f;
int n,m,last = -1;
int arr[210][210];
int a[210];
void Floyd(int now)
{
for(int k = 0;k <= now;k++)
{
for(int i = 0;i <= now;i++)
{
for(int j = 0;j <= now;j++)
{
arr[i][j] = min(arr[i][j],arr[i][k] + arr[k][j]);
}
}
}
}
void solve()
{
cin >> n >> m;
memset(arr,0x3f,sizeof(arr));
for(int i = 0;i < n;i++)
{
cin >> a[i];
}
for(int i = 1;i <= m;i++)
{
int u,v,w;
cin >> u >> v >> w;
arr[u][v] = arr[v][u] = w;
}
int q;
cin >> q;
while(q--)
{
int x,y,t;
cin >> x >> y >> t;
int l = 0,r = n;
while(l < r)
{
int mid = (l + r) >> 1;
if(a[mid] > t) r = mid;
else l = mid + 1;
}
l--;
if(x > l || y > l)
{
cout << -1 << endl;
continue;
}
if(l != last) Floyd(l);
last = l;
if(arr[x][y] > 1e9) cout << -1 << endl;
else cout << arr[x][y] << endl;
}
return;
}
signed main()
{
ios::sync_with_stdio(false),cin.tie(0);
int tt = 1;
while(tt--)
{
solve();
}
return 0;
}