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 maxp = 25;
const int maxnum = 10000001;
const int mod = 1000000007;
const int maxn = 100010;
int root = 1;
int a[maxn], u[maxn], v[maxn], x[maxn];
vector<int> prime, maxpow, g[maxn];
bool check[maxnum];
long long ans[maxn], cur[maxp][maxn];
int node[maxn], father[maxn][maxp], dep[maxn];
vector<int> power[maxn * 10], corres[maxn * 10];
void link(int u, int v) {
  g[u].push_back(v);
  g[v].push_back(u);
}
void sieve(int n) {
  for (int i = 2; i <= n; i++) {
    if (!check[i]) prime.push_back(i), maxpow.push_back(0);
    for (int j = 0; j < prime.size() && prime[j] * i <= n; j++)
      check[prime[j] * i] = true;
  }
}
void fact(int x, int id) {
  for (int i = 0; i < prime.size() && prime[i] * prime[i] <= x; i++) {
    int cnt = 0;
    while (x != 0 && x % prime[i] == 0) cnt++, x /= prime[i];
    power[i].push_back(cnt);
    corres[i].push_back(id);
  }
  if (x != 1) {
    int i = lower_bound(prime.begin(), prime.end(), x) - prime.begin();
    power[i].push_back(1);
    corres[i].push_back(id);
  }
}
inline int getpow(int x, int p) {
  int cnt = 0;
  while (x % p == 0 && x != 0) x /= p, cnt++;
  return cnt;
}
inline int qpow(int x, int p) {
  long long ret = 1LL, t = x;
  while (p) {
    if (p & 1) ret = ret * t % mod;
    t = t * t % mod;
    p >>= 1;
  }
  return ret;
}
void dfsl(int u, int p) {
  for (auto v : g[u]) {
    if (v != p) {
      father[v][0] = u;
      dep[v] = dep[u] + 1;
      dfsl(v, u);
    }
  }
}
void initlca(int n) {
  dep[root] = 0;
  dfsl(root, 0);
  for (int j = 1; j < maxp; j++) {
    for (int i = 1; i <= n; i++) {
      father[i][j] = father[father[i][j - 1]][j - 1];
    }
  }
}
int getlca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  for (int i = maxp - 1; i >= 0; i--) {
    if (dep[u] - dep[v] >= (1 << i)) u = father[u][i];
  }
  for (int i = maxp - 1; i >= 0; i--) {
    if (father[u][i] != father[v][i]) {
      u = father[u][i];
      v = father[v][i];
    }
  }
  if (u != v)
    return father[u][0];
  else
    return u;
}
void dfs(int u, int p, int c) {
  for (auto v : g[u]) {
    if (v != p) {
      cur[c][v] = cur[c][u] + node[v];
      dfs(v, u, c);
    }
  }
}
void solve(int n, int q) {
  static int p[maxn];
  initlca(n);
  for (int i = 0; i < prime.size(); i++) {
    if (power[i].size() == 0) continue;
    for (int j = 1; j <= n; j++) p[j] = getpow(a[j], prime[i]);
    for (auto j : power[i]) {
      for (int k = 1; k <= n; k++) node[k] = min(p[k], j);
      cur[j][root] = node[root];
      dfs(root, 0, j);
    }
    for (auto j : corres[i]) {
      int t = getpow(x[j], prime[i]);
      if (t == 0) continue;
      int lca = getlca(u[j], v[j]);
      long long b = cur[t][u[j]] + cur[t][v[j]] - cur[t][lca] -
                    (father[lca][0] ? cur[t][father[lca][0]] : 0);
      ans[j] = ans[j] * (long long)qpow(prime[i], b) % mod;
    }
  }
}
int main() {
  int n, q;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    link(u, v);
  }
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  scanf("%d", &q);
  int maxnum = 0;
  for (int i = 1; i <= q; i++)
    scanf("%d%d%d", &u[i], &v[i], &x[i]), maxnum = max(maxnum, x[i]),
                                          ans[i] = 1;
  sieve(maxnum);
  for (int i = 1; i <= n; i++) fact(x[i], i);
  for (int i = 0; i < prime.size(); i++)
    sort(power[i].begin(), power[i].end()),
        power[i].erase(unique(power[i].begin(), power[i].end()),
                       power[i].end());
  solve(n, q);
  for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
  return 0;
}