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;
template <typename T, typename U>
inline void smin(T &a, const U &b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
  gn(x1), gn(x2);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int flag[10000010], id[10000010];
int prime[10000010 / 10], np;
void pre() {
  for (int i = 2; i < 10000010; i++) {
    if (!flag[i]) {
      flag[i] = i;
      id[i] = np;
      prime[np++] = i;
    }
    for (int j = 0; j < np && prime[j] <= flag[i] && prime[j] * i < 10000010;
         j++)
      flag[i * prime[j]] = prime[j];
  }
}
const int mod = 1000000007;
vector<int> adj[100100];
int L[100100], R[100100], rid;
int p[100100][20], dep[100100];
void dfs(int u, int f = 0) {
  L[u] = ++rid;
  dep[u] = dep[f] + 1;
  p[u][0] = f;
  for (int i = 1; i < 20; i++) p[u][i] = p[p[u][i - 1]][i - 1];
  for (int v : adj[u]) {
    if (v == f) continue;
    dfs(v, u);
  }
  R[u] = rid;
}
int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  while (dep[u] > dep[v]) u = p[u][__builtin_ctz(dep[u] - dep[v])];
  if (u == v) return u;
  for (int i = 20; i--;) {
    if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
  }
  return p[u][0];
}
int ans[100100];
vector<pair<int, int> > num[10000010 / 10];
vector<pair<int, int> > que[10000010 / 10];
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
  return pair<int, int>(a.first + b.first, a.second + b.second);
}
pair<int, int> operator-(pair<int, int> a, pair<int, int> b) {
  return pair<int, int>(a.first - b.first, a.second - b.second);
}
int B[100100];
void update(int u, int v) {
  for (; u <= rid; u += u & -u) B[u] += v;
}
int calc(int u, int ans = 0) {
  for (; u; u -= u & -u) ans += B[u];
  return ans;
}
int U[100100], V[100100], W[100100];
int main() {
  pre();
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    gn(u, v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    int u;
    gn(u);
    while (u > 1) {
      int v = flag[u], k = 0;
      while (u % v == 0) k++, u /= v;
      num[id[v]].push_back(pair<int, int>(k, i));
    }
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int u;
    gn(U[i], V[i]), gn(u);
    W[i] = lca(U[i], V[i]);
    while (u > 1) {
      int v = flag[u], k = 0;
      while (u % v == 0) k++, u /= v;
      que[id[v]].push_back(pair<int, int>(k, i));
    }
    ans[i] = 1;
  }
  for (int i = 0; i < np; i++)
    if (!que[i].empty() && !num[i].empty()) {
      sort(que[i].begin(), que[i].end());
      for (int j = 0, k = 0; j < que[i].size(); j = k) {
        while (k < que[i].size() && que[i][j].first == que[i][k].first) k++;
        int d = que[i][j].first;
        for (pair<int, int> p : num[i]) {
          update(L[p.second], min(d, p.first));
          update(R[p.second] + 1, -min(d, p.first));
        }
        for (; j < k; j++) {
          int id = que[i][j].second;
          int ct = calc(L[U[id]]) + calc(L[V[id]]) - calc(L[W[id]]) -
                   calc(L[p[W[id]][0]]);
          ans[id] = (long long)ans[id] * power(prime[i], ct, mod) % mod;
        }
        for (pair<int, int> p : num[i]) {
          update(L[p.second], -min(d, p.first));
          update(R[p.second] + 1, min(d, p.first));
        }
      }
    }
  for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
  return 0;
}