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 P = 1e9 + 7;
const int MAXN = 1e5 + 10;
const int MAXP = 1e6 + 10;
const int MAXV = 1e7 + 10;
struct edge {
  int to, nxt;
} e[MAXN << 1];
struct info {
  int value, home;
  bool type;
};
int tot, n, t;
int a[MAXN], depth[MAXN], prime[MAXP], head[MAXN], ans[MAXN], f[MAXV];
int cnt[MAXP][30], anc[MAXN][30];
vector<info> q[MAXN];
template <typename T>
inline void chkmax(T &x, T y) {
  x = max(x, y);
}
template <typename T>
inline void chkmin(T &x, T y) {
  x = min(x, y);
}
template <typename T>
inline void read(T &x) {
  T f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
  x *= f;
}
inline void addedge(int u, int v) {
  t++;
  e[t] = (edge){v, head[u]};
  head[u] = t;
}
inline int power(int a, int n) {
  int res = 1, b = a;
  while (n > 0) {
    if (n & 1) res = 1ll * res * b % P;
    b = 1ll * b * b % P;
    n >>= 1;
  }
  return res;
}
inline int inv(int x) { return power(x, P - 2); }
inline void dfs(int u, int fa) {
  depth[u] = depth[fa] + 1;
  for (int i = 1; i < 30; i++) {
    if (depth[u] <= (1 << i)) break;
    anc[u][i] = anc[anc[u][i - 1]][i - 1];
  }
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == fa) continue;
    anc[v][0] = u;
    depth[v] = depth[u] + 1;
    dfs(v, u);
  }
}
inline int lca(int u, int v) {
  if (depth[u] > depth[v]) swap(u, v);
  for (int i = 30 - 1; i >= 0; i--) {
    if (depth[anc[v][i]] >= depth[u]) v = anc[v][i];
  }
  if (u == v) return u;
  for (int i = 30 - 1; i >= 0; i--) {
    if (anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i];
  }
  return anc[u][0];
}
inline void modify(int x, int delta) {
  for (int i = 1; 1ll * prime[i] * prime[i] <= x; i++) {
    if (x % prime[i] == 0) {
      int p = 0;
      while (x % prime[i] == 0) {
        x /= prime[i];
        p++;
      }
      cnt[i][p] += delta;
    }
  }
  if (x != 1) {
    int pos = lower_bound(prime + 1, prime + tot + 1, x) - prime;
    cnt[pos][1] += delta;
  }
}
inline int query(int x) {
  int ret = 1;
  for (int i = 1; 1ll * prime[i] * prime[i] <= x; i++) {
    if (x % prime[i] == 0) {
      int p = 0;
      while (x % prime[i] == 0) {
        p++;
        x /= prime[i];
      }
      int s = 0;
      for (int j = 1; j <= p; j++) s += cnt[i][j] * j;
      for (int j = p + 1; j < 30; j++) s += cnt[i][j] * p;
      ret = 1ll * ret * power(prime[i], s) % P;
    }
  }
  if (x != 1) {
    int pos = lower_bound(prime + 1, prime + tot + 1, x) - prime;
    int s = 0;
    for (int i = 1; i < 30; i++) s += cnt[pos][i];
    ret = 1ll * ret * power(x, s) % P;
  }
  return ret;
}
inline void solve(int u, int fa) {
  modify(a[u], 1);
  for (unsigned i = 0; i < q[u].size(); i++) {
    if (q[u][i].type)
      ans[q[u][i].home] = 1ll * ans[q[u][i].home] * query(q[u][i].value) % P;
    else
      ans[q[u][i].home] =
          1ll * ans[q[u][i].home] * inv(query(q[u][i].value)) % P;
  }
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == fa) continue;
    solve(v, u);
  }
  modify(a[u], -1);
}
int main() {
  read(n);
  for (int i = 2; i < MAXV; i++) {
    if (!f[i]) {
      f[i] = i;
      prime[++tot] = i;
    }
    for (int j = 1; j <= tot; j++) {
      int tmp = i * prime[j];
      if (tmp >= MAXV) break;
      f[tmp] = prime[j];
      if (prime[j] == f[i]) break;
    }
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    read(u);
    read(v);
    addedge(u, v);
    addedge(v, u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) read(a[i]);
  int Q;
  read(Q);
  for (int i = 1; i <= Q; i++) {
    int u, v, x;
    read(u);
    read(v);
    read(x);
    int Lca = lca(u, v);
    q[u].push_back((info){x, i, true});
    q[v].push_back((info){x, i, true});
    q[Lca].push_back((info){x, i, false});
    if (Lca != 1) q[anc[Lca][0]].push_back((info){x, i, false});
    ans[i] = 1;
  }
  solve(1, 0);
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
  return 0;
}