Problem: Let the main characters of this problem be personages from some recent movie. New Avengers seem to make a lot of buzz. I didn't watch any part of the franchise and don't know its heroes well, but it won't stop me from using them in this problem statement. So, Thanos and Dr. Strange are doing their superhero and supervillain stuff, but then suddenly they stumble across a regular competitive programming problem.

You are given a tree with n vertices.

In each vertex v there is positive integer a_{v}.

You have to answer q queries.

Each query has a from u v x.

You have to calculate ∏_{w ∈ P} gcd(x, a_{w}) mod (10^{9} + 7), where P is a set of vertices on path from u to v. In other words, you are to calculate the product of gcd(x, a_{w}) for all vertices w on the path from u to v. As it might be large, compute it modulo 10^9+7. Here gcd(s, t) denotes the greatest common divisor of s and t.

Note that the numbers in vertices do not change after queries.

I suppose that you are more interested in superhero business of Thanos and Dr. Strange than in them solving the problem. So you are invited to solve this problem instead of them.

Input

In the first line of input there is one integer n (1 ≤ n ≤ 10^{5}) — the size of the tree.

In the next n-1 lines the edges of the tree are described. The i-th edge is described with two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n) and it connects the vertices u_{i} and v_{i}. It is guaranteed that graph with these edges is a tree.

In the next line there are n integers a_1, a_2, …, a_n (1 ≤ a_{v} ≤ 10^{7}).

In the next line there is one integer q (1 ≤ q ≤ 10^{5}) — the number of queries.

And in the next q lines the queries are described. Each query is described with three integers u_{i}, v_{i} and x_{i} (1 ≤ u_{i}, v_{i} ≤ n, 1 ≤ x_{i} ≤ 10^{7}).

Output

Print q numbers — the answers to the queries in the order they are given in the input. Print each answer modulo 10^9+7 = 1000000007. Print each number on a separate line.

Examples

Input

4
1 2
1 3
1 4
6 4 9 5
3
2 3 6
2 3 2
3 4 7


Output

36
4
1


Input

6
1 2
2 3
2 4
1 5
5 6
100000 200000 500000 40000 800000 250000
3
3 5 10000000
6 2 3500000
4 1 64000


Output

196000
12250
999998215

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
const int MAXLG = 18;
const int MAXVAL = 10000000;
const int MAXQ = 100000;
const int MOD = 1000000007;
struct E {
  int pw, qid, nodeid;
  E() {}
  E(int pw, int qid, int nodeid) : pw(pw), qid(qid), nodeid(nodeid) {}
};
bool operator<(const E &a, const E &b) {
  if (a.pw != b.pw) return a.pw < b.pw;
  if ((a.qid == -1) != (b.qid == -1)) return a.qid == -1;
  return false;
}
int pw(int x, int n) {
  int ret = 1;
  while (true) {
    if (n & 1) ret = (long long)ret * x % MOD;
    if ((n >>= 1) == 0) return ret;
    x = (long long)x * x % MOD;
  }
}
int n;
vector<int> adj[MAXN];
int val[MAXN];
int nq;
int qa[MAXQ], qb[MAXQ], qx[MAXQ];
int ans[MAXQ];
int pmn[MAXVAL + 1];
int par[MAXN], dep[MAXN];
int lid[MAXN], rid[MAXN], nid;
int up[MAXN][MAXLG + 1];
vector<E> e[MAXVAL + 1];
int bit[2 * MAXN + 1];
void dfsinit(int at) {
  lid[at] = nid++;
  dep[at] = par[at] == -1 ? 0 : dep[par[at]] + 1;
  up[at][0] = par[at] == -1 ? at : par[at];
  for (int k = (0); k < (MAXLG); ++k) up[at][k + 1] = up[up[at][k]][k];
  for (int i = (0); i < (((int)(adj[at]).size())); ++i) {
    int to = adj[at][i];
    if (to == par[at]) continue;
    par[to] = at;
    dfsinit(to);
  }
  rid[at] = nid++;
}
int lca(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  for (int k = MAXLG; k >= 0; --k)
    if (dep[a] - (1 << k) >= dep[b]) a = up[a][k];
  for (int k = MAXLG; k >= 0; --k)
    if (up[a][k] != up[b][k]) a = up[a][k], b = up[b][k];
  return a == b ? a : up[a][0];
}
void bmod(int idx, int by) {
  ++idx;
  while (idx <= nid) {
    bit[idx] += by;
    idx += idx & -idx;
  }
}
int bget(int idx) {
  ++idx;
  int ret = 0;
  while (idx > 0) {
    ret += bit[idx];
    idx -= idx & -idx;
  }
  return ret;
}
int bget(int lidx, int ridx) { return bget(ridx) - bget(lidx - 1); }
void run() {
  scanf("%d", &n);
  for (int i = (0); i < (n - 1); ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a, --b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = (0); i < (n); ++i) scanf("%d", &val[i]);
  scanf("%d", &nq);
  for (int i = (0); i < (nq); ++i)
    scanf("%d%d%d", &qa[i], &qb[i], &qx[i]), --qa[i], --qb[i];
  memset(pmn, -1, sizeof(pmn));
  for (int i = (2); i <= (MAXVAL); ++i)
    if (pmn[i] == -1)
      for (int j = i; j <= MAXVAL; j += i)
        if (pmn[j] == -1) pmn[j] = i;
  nid = 0;
  par[0] = -1;
  dfsinit(0);
  for (int i = (0); i < (n); ++i) {
    int x = val[i];
    while (x != 1) {
      int p = pmn[x], pw = 0;
      while (x % p == 0) x /= p, ++pw;
      for (int k = (1); k <= (pw); ++k) e[p].push_back(E(k, -1, i));
    }
  }
  for (int i = (0); i < (nq); ++i) {
    int x = qx[i];
    while (x != 1) {
      int p = pmn[x], pw = 0;
      while (x % p == 0) x /= p, ++pw;
      e[p].push_back(E(pw, i, -1));
    }
  }
  for (int i = (0); i < (nq); ++i) ans[i] = 1;
  memset(bit, 0, sizeof(bit));
  for (int p = (2); p <= (MAXVAL); ++p)
    if (((int)(e[p]).size()) != 0) {
      sort(e[p].begin(), e[p].end());
      for (int i = (0); i < (((int)(e[p]).size())); ++i) {
        if (e[p][i].qid != -1) {
          int qid = e[p][i].qid;
          int a = qa[qid], b = qb[qid];
          if (lid[a] > lid[b]) swap(a, b);
          int c = lca(a, b);
          int sum1 = -bget(rid[a], rid[c]), sum2 = bget(lid[c], lid[b]),
              sum3 = bget(lid[c], lid[c]), sum = sum1 + sum2 - sum3;
          ans[qid] = (long long)ans[qid] * pw(p, sum) % MOD;
        }
        if (e[p][i].nodeid != -1) {
          int at = e[p][i].nodeid;
          bmod(lid[at], +1);
          bmod(rid[at], -1);
        }
      }
      for (int i = (0); i < (((int)(e[p]).size())); ++i) {
        if (e[p][i].nodeid != -1) {
          int at = e[p][i].nodeid;
          bmod(lid[at], -1);
          bmod(rid[at], +1);
        }
      }
    }
  for (int i = (0); i < (nq); ++i) printf("%d\n", ans[i]);
}
int main() {
  run();
  return 0;
}