Problem: G: Travel Support-Travel Support-

story

I, Honoka Kosaka, 16 years old! I'm a student idol! !!

In order to give a live performance to convey the goodness of student idols to everyone, we invite many student idols to perform live at Akiba Stadium.

However, some live participants come from afar and it costs a lot of train fare. That's why I decided to work part-time as a member of u ’s and give the train fare to the live participants! However, because the part-time job fee is late, it may not be possible to give the train fare in time for the departure of the live participants, or the part-time job fee may not be enough to give the full amount of the train fare. In such a case, I ask the live participants to prepare enough money in advance, but how much should I prepare?

So I want you to ask for the minimum amount that a live participant needs to have!

Mai-chan, rent a train?

problem

There are N cities, each numbered from 1 to N. The i (1 ≤ i ≤ N) th city has a population of t_i (t_i ≠ t_j if i ≠ j).

Currently, K live participants are about to gather in City 1. There are M ways of transportation allowed, and the i-th transportation can move between cities a_i and city b_i (1 ≤ a_i, b_i ≤ N) in both directions at the cost of c_i yen. It takes one day to travel by any means of transportation.

The i (1 ≤ i ≤ K) th participant departs from city x_i and heads for city 1 on a route that minimizes the cost required. If there are multiple routes with the lowest cost, select the route with the least number of travel days. If there are still multiple routes, choose a route that gives priority to moving to a city with a small population. Since this will determine the route and the number of days until arrival, each participant will start moving so that they will arrive at City 1 on the day of the live performance.

It is also known that the i (1 ≤ i ≤ K) th participant will be paid p_i yen d_i days before the live performance. As much as possible, the expenses paid will be used for movement after payment, but each participant must prepare in advance the costs incurred before payment and the costs after payment that cannot be covered.

Find the minimum cost that each participant will prepare in advance.

Input format

The input is given in the following format.


N M
t_1 ... t_N
a_1 b_1 c_1
...
a_M b_M c_M
K
x_1 d_1 p_1
...
x_K d_K p_K


Two integers N (1 ≤ N ≤ 100,000) and M (0 ≤ M ≤ 500,000) are given on the first line, separated by blanks, and represent the number of cities and the number of modes of transportation allowed, respectively. The second line is given N integers t_i (1 ≤ i ≤ N, 1 ≤ t_i ≤ 500,000) separated by blanks. t_i represents the population of city i, and if i ≠ j, then t_i ≠ t_j is satisfied.

The following M line is the input for the means of transportation, and three integers a_i, b_i, and c_i are given for the i-th means of transportation, separated by blanks. This means that the i-th means of transportation can travel between cities a_i and b_i (1 ≤ a_i, b_i ≤ N, a_i ≠ b_i) and costs c_i (1 ≤ c_i ≤ 10,000) yen. Also, there is no more than one means of transportation that directly connects any two cities. All cities can travel to and from each other using several modes of transportation.

The next line is given the integer K (1 ≤ K ≤ 100,000), which represents the number of participants.

The following K line is the input for the participants, and three integers x_i, d_i, p_i are given for the i-th participant, separated by blanks. This means that the i-th participant is initially in the city x_i (1 ≤ x_i ≤ N) and will be paid p_i (0 ≤ p_i ≤ 100,000) yen one day before the live d_i (0 ≤ d_i ≤ 100,000). Represent.

Output format

Output the minimum cost prepared by each participant in advance, starting with the first participant, separated by line breaks.

Since the input / output can be very large, it is recommended to use a high-speed input / output function.

Input example 1


5 6
100 80 70 60 50
1 2 500
2 5 100
1 3 400
1 4 200
3 5 700
4 5 800
1
5 3 600


Output example 1


0

The minimum cost for the entire trip is 600 yen, and you can reach City 1 in 2 days. There is no need to prepare the cost in advance as the cost will be paid 3 days in advance.

Input example 2


5 6
400 200 500 300 100
1 2 500
2 5 100
1 3 400
1 4 200
3 5 200
4 5 800
1
5 1 800


Output example 2


100

There are two routes with the lowest cost and the smallest number of days, 5-2-1 and 5-3-1. When moving from city 5 to the next city, city 2 is more important than city 3. Since the population is smaller, route 5-2-1 is selected. The total cost can be paid in full with the payment amount of 600 yen, but the cost before the payment must be reimbursed, so it is necessary to prepare only 100 yen, which is the cost between 5-2.

Input example 3


10 13
100 90 80 70 60 50 40 30 20 10
1 2 5
1 4 4
2 3 3
3 5 2
4 5 6
4 6 7
4 7 2
5 8 1
5 9 8
6 7 10
6 9 7
6 10 3
7 10 10
Ten
2 0 0
2 1 3
3 0 100000
3 1 3
3 1 100000
3 2 100000
3 100000 100000
8 1 5
9 2 11
10 0 0


Output example 3


Five
2
8
Five
3
0
0
7
7
14






Example

Input

5 6
100 80 70 60 50
1 2 500
2 5 100
1 3 400
1 4 200
3 5 700
4 5 800
1
5 3 600


Output

0

Solution (in CPP):

Solution:
#include "bits/stdc++.h"
using namespace std;
vector<int>pops;
struct edge {
	int from;
	int to;
	long long int cost;
};
struct aa {
	edge frome;
	long long int cost;
	 int time;
};
class Compare {
public:
	bool operator()(const aa l, const aa r) {
		return l.cost == r.cost ?l.time==r.time?pops[l.frome.from] > pops[r.frome.from]: l.time > r.time:l.cost > r.cost;
	}
}comp;	


class Tree {
public:
	Tree(int V, int root) : V(V),revT(V), root(root), cnum(V), place(V), id(V) {
		T.resize(V);
		for (int i = 0; i < MAXLOGV; i++) {
			parent[i].resize(V);
			costs[i].resize(V);

		}
		depth.resize(V);
	}
	// u??¨v????????????
	// lca????±????????????¨????????????????????§????????°????????¨????????????
	void unite(edge& v) {
		T[v.from].push_back(v);
		revT[v.to]=(edge{ v.to,v.from,v.cost });
	}
	// init??????
	// ????????????????????????????????????????????????????????°????????¨lca????±???????????????????
	void init() {
		dfs(root, 0, 0);
		int id = 0;
		getid(root, 0, id);
	}
	// u??¨v???lca????±???????
	int lca(int u, int v) const {
		if (depth[u] > depth[v]) swap(u, v);
		for (int k = 0; k < MAXLOGV; k++) {
			if ((depth[v] - depth[u]) >> k & 1) {
				v = parent[k][v];
			}
		}
		if (u == v) return u;
		for (int k = MAXLOGV - 1; k >= 0; k--) {
			if (parent[k][u] != parent[k][v]) {
				u = parent[k][u];
				v = parent[k][v];
			}
		}
		return parent[0][u];
	}
	// u??¨v????????¢????±???????
	// edge????????????????????¨????????????????????????????????????
	int dist(int u, int v) const {
		int p = lca(u, v);
		return (depth[u] - depth[p]) + (depth[v] - depth[p]);
	}
	int dfs(int v, int p, int d) {
		parent[0][v] = p;
		costs[0][v] = revT[v].cost;
		depth[v] = d;
		cnum[v] = 0;
		for (int i = 1; i < MAXLOGV; i++) {
			parent[i][v] = parent[i - 1][parent[i - 1][v]];
			costs[i][v] = costs[i - 1][parent[i - 1][v]] + costs[i - 1][v];
		}
		for (auto e : T[v]) {
			int next = e.to;
			if (next != p) cnum[v] += dfs(next, v, d + 1);
		}
		return cnum[v] + 1;
	}

	void getid(const int v, const int p, int &nplace) {
		place[v] = nplace;
		id[nplace] = v;
		nplace++;
		for (auto e : T[v]) {
			int next = e.to;
			if (next != p) getid(next, v, nplace);
		}
	}
	static const int MAXLOGV = 20;
	// ??°???????????£??\???????????¨???
	vector<vector<edge> > T;
	vector<edge>revT;
	// ???????????°
	int V;
	// ?????????????????????
	int root;

	// ????????????
	vector<int> parent[MAXLOGV];
	vector<long long int>costs[MAXLOGV];
	// ??????????????±???
	vector<int> depth;

	//????????°
	vector<int>cnum;

	//??????
	vector<int>place;
	vector<int>id;

};

int main() {
	int N, M; cin >> N >> M;
	for (int i = 0; i < N; ++i) {
		int a; cin >> a;
		pops.push_back(a);
	}
	vector<vector<edge>>edges(N);
	for (int i = 0; i < M; ++i) {
		int a, b, c; cin >> a >> b >> c; a--; b--;
		edges[a].push_back(edge{ a,b,c });
		edges[b].push_back(edge{ b,a,c });
	}
	
	vector<aa>memo(N, aa{ edge{-1,-1,-1},static_cast<long long int>(1e18),(static_cast<int>(1e9)) });
	memo[0] = aa{ edge{-1,0,0},0,0 };
	priority_queue<aa, vector<aa>, Compare>que;
	que.push(aa{ edge{-1,0,0},0,0 });
	while (!que.empty()) {
		aa atop(que.top());
		que.pop();
		for (auto e : edges[atop.frome.to]) {
			const int next = e.to;
			const long long int nextcost = atop.cost + e.cost;
			const int nexttime = atop.time + 1;
			if (comp.operator()(memo[next],aa{ e,nextcost,nexttime })) {
				memo[next] = aa{ e,nextcost,nexttime };
				que.push(aa{ e,nextcost,nexttime });
			}
		}
	}
	Tree t(N,0);
	for (int i = 1; i < N; ++i) {
		t.unite(memo[i].frome);
	}
	t.init();
	int K; cin >> K;
	for (int i = 0; i < K; ++i) {
		long long int sum = 0;
		int x, d, p; cin >> x >> d >> p;
		x--;
		if (d >= t.depth[x]) {
			int rest = t.depth[x];
			long long int need = 0;
			int now = x;
			for (int i = 0; i < 20; ++i) {
				if (rest&(1 << i)) {
					need += t.costs[i][now];
					now = t.parent[i][now];
				}
			}
			sum += max(0ll, need -p);
		}
		else {
			int rest = t.depth[x] - d;
			long long int need = 0;
			int now = x;
			for (int i = 0; i < 20; ++i) {
				if (rest&(1 << i)) {
					need += t.costs[i][now];
					now = t.parent[i][now];
				}
			}
			sum += need;
			rest = d;
			need = 0;
			for (int i = 0; i < 20; ++i) {
				if (rest&(1 << i)) {
					need += t.costs[i][now];
					now = t.parent[i][now];
				}
			}
			sum += max(0ll, need - p);
		}
		cout << sum << endl;
	}
	return 0;
}