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;
const int N = 1e5 + 5, P = 1e7 + 1, M = 7e5 + 5;
int p[M], cnt, firp[P], id[P], val[M], m, n, q, u, v, edgenum, Head[N],
    Next[N << 1], vet[N << 1], w[N], x, len[N], dep[N], ST[N][20], now[M],
    tmp[N * 30], c;
pair<int, int> qa[N][30];
bool isnp[P];
vector<pair<int, int> > query[N];
inline void add(int u, int v) {
  Next[++edgenum] = Head[u];
  Head[u] = edgenum;
  vet[edgenum] = v;
}
inline int qpow(int x, int y) {
  int t = 1;
  while (y) {
    if (y & 1) t = 1ll * t * x % 1000000007;
    x = 1ll * x * x % 1000000007, y >>= 1;
  }
  return t;
}
inline void getPrime() {
  for (int i = 2; i <= P - 1; i++) {
    if (!isnp[i]) {
      cnt++, p[cnt] = firp[i] = i, id[i] = m + 1;
      for (long long j = i; j <= P - 1; j *= i) val[++m] = i;
    }
    for (int j = 1; j <= cnt && i * p[j] <= P - 1; j++) {
      isnp[i * p[j]] = 1, firp[i * p[j]] = p[j];
      if (!(i % p[j])) break;
    }
  }
}
void dfs1(int u, int fa) {
  ST[u][0] = fa, dep[u] = dep[fa] + 1;
  for (int i = 1; 1 << i < dep[u]; i++) ST[u][i] = ST[ST[u][i - 1]][i - 1];
  for (int e = Head[u]; e; e = Next[e]) {
    int v = vet[e];
    if (v ^ fa) dfs1(v, u);
  }
}
inline int getLCA(int u, int v) {
  if (u == v) return u;
  if (dep[u] < dep[v]) swap(u, v);
  int k = dep[u] - dep[v];
  for (int i = 0; i <= 17; i++)
    if (k & 1 << i) {
      u = ST[u][i];
      if (u == v) return u;
    }
  for (int i = 17; ~i; i--)
    if (ST[u][i] ^ ST[v][i]) u = ST[u][i], v = ST[v][i];
  return ST[u][0];
}
void dfs2(int u, int fa) {
  int lst = 0, cnt = 0, x = w[u], _c = c;
  while (x ^ 1) {
    int _ = firp[x];
    if (_ == lst)
      ++cnt;
    else
      lst = _, cnt = 0;
    now[tmp[++c] = id[_] + cnt]++;
    x /= _;
  }
  for (auto &i : query[u])
    for (int j = 1; j <= len[i.first]; j++)
      qa[i.first][j].second += i.second * now[qa[i.first][j].first];
  for (int e = Head[u]; e; e = Next[e]) {
    int v = vet[e];
    if (v ^ fa) dfs2(v, u);
  }
  while (c > _c) now[tmp[c--]]--;
}
int main() {
  getPrime();
  cin >> n;
  for (int i = 1; i < n; i++) scanf("%d%d", &u, &v), add(u, v), add(v, u);
  for (int i = 1; i <= n; i++) scanf("%d", &w[i]);
  dfs1(1, 0);
  cin >> q;
  for (int i = 1; i <= q; i++) {
    scanf("%d%d%d", &u, &v, &x);
    int LCA = getLCA(u, v);
    if (LCA == u)
      query[v].push_back(pair<int, int>(i, 1)),
          query[ST[u][0]].push_back(pair<int, int>(i, -1));
    else if (LCA == v)
      query[u].push_back(pair<int, int>(i, 1)),
          query[ST[v][0]].push_back(pair<int, int>(i, -1));
    else
      query[u].push_back(pair<int, int>(i, 1)),
          query[v].push_back(pair<int, int>(i, 1)),
          query[LCA].push_back(pair<int, int>(i, -1)),
          query[ST[LCA][0]].push_back(pair<int, int>(i, -1));
    int lst = 0, cnt = 0;
    while (x ^ 1) {
      int _ = firp[x];
      if (_ == lst)
        ++cnt;
      else
        lst = _, cnt = 0;
      qa[i][++len[i]] = pair<int, int>(id[_] + cnt, 0);
      x /= _;
    }
  }
  dfs2(1, 0);
  for (int i = 1; i <= q; i++) {
    int ans = 1;
    for (int j = 1; j <= len[i]; j++)
      ans = 1ll * ans * qpow(val[qa[i][j].first], qa[i][j].second) % 1000000007;
    printf("%d\n", ans);
  }
  return 0;
}