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>
int n, qr;
int v[1 + 100000];
struct Query {
  int ind, sign;
};
std::vector<Query> Q[1 + 100000];
std::vector<int> G[1 + 100000];
struct PRIME {
  int val, exp, nexp;
};
struct query {
  int u, v, x;
  std::vector<PRIME> pr;
} q[1 + 100000];
std::vector<PRIME> V[1 + 100000];
int parcurgere[1 + 2 * 100000], ind;
int depth[1 + 100000], first[1 + 100000];
int fth[1 + 100000];
void dfs(int node, int papa) {
  fth[node] = papa;
  depth[node] = 1 + depth[papa];
  parcurgere[++ind] = node;
  first[node] = ind;
  for (auto y : G[node])
    if (y != papa) {
      dfs(y, node);
      parcurgere[++ind] = node;
    }
}
int rmq[1 + 2 * 100000][1 + 20];
int lg[1 + 2 * 100000];
inline void buildRmq() {
  lg[1] = 0;
  for (int i = 2; i <= ind; i++) lg[i] = 1 + lg[i / 2];
  for (int i = 1; i <= ind; i++) rmq[i][0] = parcurgere[i];
  for (int len = 1; len <= 20; len++)
    for (int i = 1; i <= ind && i + (1 << len) - 1 <= ind; i++)
      if (depth[rmq[i][len - 1]] < depth[rmq[i + (1 << (len - 1))][len - 1]])
        rmq[i][len] = rmq[i][len - 1];
      else
        rmq[i][len] = rmq[i + (1 << (len - 1))][len - 1];
}
inline int Rmq(int x, int y) {
  int a = first[x], b = first[y];
  if (a > b) std::swap(a, b);
  int len = lg[b - a];
  if (depth[rmq[a][len]] < depth[rmq[b - (1 << len) + 1][len]])
    return rmq[a][len];
  return rmq[b - (1 << len) + 1][len];
}
struct Fenwick {
  int *aib, sz, cnt;
  Fenwick(int dim) {
    sz = dim;
    cnt = 0;
    aib = new int[1 + dim];
    memset(aib, 0, (1 + dim) * sizeof(int));
  }
  inline void update(int pos, int val) {
    for (int i = pos; i <= sz; i += (i & (-i))) aib[i] += val;
    cnt += val;
  }
  inline int query(int pos) {
    int ans = 0;
    for (int i = pos; i > 0; i -= (i & (-i))) ans += aib[i];
    return ans;
  }
};
Fenwick *F[1 + 700000];
Fenwick *N[1 + 700000];
int ciur[1 + 10000000];
int poz[1 + 10000000], cnt;
std::vector<int> prime;
inline void Ciur() {
  for (int i = 1; i <= 10000000; i++) ciur[i] = 1000000000;
  for (int i = 2; i <= 10000000; i++) {
    if (ciur[i] == 1000000000) {
      prime.push_back(i);
      cnt++;
      poz[i] = cnt;
      if (i < 10)
        F[cnt] = new Fenwick(30), N[cnt] = new Fenwick(30);
      else if (i < 100)
        F[cnt] = new Fenwick(7), N[cnt] = new Fenwick(7);
      else
        F[cnt] = new Fenwick(4), N[cnt] = new Fenwick(4);
      ciur[i] = i;
    }
    for (int j = 0;
         j < prime.size() && i * prime[j] <= 10000000 && prime[j] <= ciur[i];
         j++)
      ciur[i * prime[j]] = prime[j];
  }
}
void compute(int node, int papa) {
  for (auto y : V[node]) {
    F[poz[y.val]]->update(y.exp, +y.exp);
    N[poz[y.val]]->update(y.exp, +1);
  }
  for (auto y : Q[node]) {
    for (int z = 0; z < q[y.ind].pr.size(); z++) {
      auto T = q[y.ind].pr[z];
      q[y.ind].pr[z].nexp +=
          y.sign *
          (F[poz[T.val]]->query(T.exp - 1) +
           +T.exp * (N[poz[T.val]]->cnt - N[poz[T.val]]->query(T.exp - 1)));
    }
  }
  for (auto y : G[node])
    if (y != papa) compute(y, node);
  for (auto y : V[node]) {
    F[poz[y.val]]->update(y.exp, -y.exp);
    N[poz[y.val]]->update(y.exp, -1);
  }
}
inline long long logpow(long long a, long long n) {
  long long p = 1;
  while (n) {
    if (n % 2) p = (p * a) % 1000000007;
    a = (a * a) % 1000000007;
    n /= 2;
  }
  return p;
}
int main() {
  FILE *fi, *fo;
  fi = stdin;
  fo = stdout;
  Ciur();
  fscanf(fi, "%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    fscanf(fi, "%d%d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(1, 0), buildRmq();
  for (int i = 1; i <= n; i++) {
    fscanf(fi, "%d", &v[i]);
    int x = v[i];
    int prev = 1;
    while (x != 1) {
      int next = ciur[x];
      if (next == prev)
        V[i][V[i].size() - 1].exp++;
      else
        V[i].push_back({next, 1, 0});
      prev = next;
      x /= next;
    }
  }
  fscanf(fi, "%d", &qr);
  for (int i = 1; i <= qr; i++) {
    fscanf(fi, "%d%d%d", &q[i].u, &q[i].v, &q[i].x);
    Q[q[i].u].push_back({i, 1});
    Q[q[i].v].push_back({i, 1});
    int lca = Rmq(q[i].u, q[i].v);
    Q[lca].push_back({i, -1});
    Q[fth[lca]].push_back({i, -1});
    int x = q[i].x;
    int prev = 1;
    while (x != 1) {
      int next = ciur[x];
      if (next == prev)
        q[i].pr[q[i].pr.size() - 1].exp++;
      else
        q[i].pr.push_back({next, 1, 0});
      prev = next;
      x /= next;
    }
  }
  compute(1, 0);
  for (int i = 1; i <= qr; i++) {
    long long acc = 1;
    for (auto y : q[i].pr) acc = (acc * logpow(y.val, y.nexp)) % 1000000007;
    fprintf(fo, "%lld\n", acc);
  }
  return 0;
}