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 long long MOD = 1e9 + 7;
inline long long quickPow(long long x, int exp) {
  long long ret = 1, tmp = x;
  while (exp) {
    if (exp & 1) ret = ret * tmp % MOD;
    tmp = tmp * tmp % MOD;
    exp >>= 1;
  }
  return ret;
}
inline long long getInv(long long x) { return quickPow(x, MOD - 2); }
const int MAX = 1e7;
int prime[1000005], cntPrime, primeID[MAX];
bool notPrime[MAX];
inline void getPrimes() {
  for (int i = 2; i < MAX; ++i) {
    if (!notPrime[i]) {
      prime[++cntPrime] = i;
      primeID[i] = cntPrime;
    }
    for (int j = 1; j <= cntPrime && i * prime[j] < MAX; ++j) {
      notPrime[i * prime[j]] = 1;
      if (i % prime[j] == 0) break;
    }
  }
}
int n, q;
int a[100005];
vector<int> G[100005];
vector<pair<int, int> > qry[100005];
long long ans[100005];
int dep[100005], fa[100005][18];
inline void dfs(int u, int pa) {
  fa[u][0] = pa;
  dep[u] = dep[pa] + 1;
  for (int i = 0; i < G[u].size(); ++i) {
    int v = G[u][i];
    if (v != pa) dfs(v, u);
  }
}
inline int LCA(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  int delta = dep[u] - dep[v];
  for (int j = 16; j >= 0; --j)
    if (delta & (1 << j)) u = fa[u][j];
  if (u == v) return u;
  for (int j = 16; j >= 0; --j)
    if (fa[u][j] != fa[v][j]) u = fa[u][j], v = fa[v][j];
  return fa[u][0];
}
vector<int> num[1000005];
inline void getAns(int u, int pa) {
  int T = a[u];
  for (int i = 1; prime[i] * prime[i] <= T; ++i) {
    int cnt = 0;
    while (T % prime[i] == 0) ++cnt, T /= prime[i];
    for (int j = 0; j < num[i].size(); ++j) num[i][j] += min(j + 1, cnt);
  }
  if (T > 1) {
    int id = primeID[T];
    for (int j = 0; j < num[id].size(); ++j) num[id][j]++;
  }
  for (int i = 0; i < qry[u].size(); ++i) {
    int x = qry[u][i].first, qid = qry[u][i].second;
    long long ret = 1;
    for (int j = 1; prime[j] * prime[j] <= x; ++j) {
      int cnt = 0;
      while (x % prime[j] == 0) x /= prime[j], ++cnt;
      if (cnt) ret = ret * quickPow(prime[j], num[j][cnt - 1]) % MOD;
    }
    if (x > 1) {
      int id = primeID[x];
      ret = ret * quickPow(prime[id], num[id][0]) % MOD;
    }
    if (qid > 0)
      ans[qid] = ans[qid] * ret % MOD;
    else
      ans[-qid] = ans[-qid] * getInv(ret) % MOD;
  }
  for (int i = 0; i < G[u].size(); ++i) {
    int v = G[u][i];
    if (v != pa) getAns(v, u);
  }
  T = a[u];
  for (int i = 1; prime[i] * prime[i] <= T; ++i) {
    int cnt = 0;
    while (T % prime[i] == 0) ++cnt, T /= prime[i];
    for (int j = 0; j < num[i].size(); ++j) num[i][j] -= min(j + 1, cnt);
  }
  if (T > 1) {
    int id = primeID[T];
    for (int j = 0; j < num[id].size(); ++j) num[id][j]--;
  }
}
int main() {
  ios::sync_with_stdio(false);
  getPrimes();
  for (int i = 1; i <= cntPrime; ++i) {
    int cnt = 0;
    long long T = prime[i];
    while (T <= MAX) ++cnt, T *= prime[i];
    num[i].resize(cnt);
  }
  cin >> n;
  for (int i = 1; i <= (n - 1); ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 0);
  for (int j = 0; j < 16; ++j)
    for (int i = 1; i <= (n); ++i) fa[i][j + 1] = fa[fa[i][j]][j];
  for (int i = 1; i <= (n); ++i) cin >> a[i];
  cin >> q;
  for (int i = 1; i <= (q); ++i) {
    ans[i] = 1;
    int u, v, x;
    cin >> u >> v >> x;
    qry[u].push_back(make_pair(x, i));
    qry[v].push_back(make_pair(x, i));
    int lca = LCA(u, v);
    qry[lca].push_back(make_pair(x, -i));
    if (lca != 1) qry[fa[lca][0]].push_back(make_pair(x, -i));
  }
  getAns(1, 0);
  for (int i = 1; i <= (q); ++i) cout << ans[i] << endl;
  return 0;
}