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;
inline int gi() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
  return sum;
}
const int maxn = 100005, mod = 1e9 + 7, maxv = 1e7 + 5, S = sqrt(1e7);
int n, q, val[maxn], ans[maxn];
int p[maxv], pcnt, d[maxv];
vector<pair<int, int>> Inc[maxn], Dec[maxn];
int tmp[maxv * 2], *g[maxv], Log[maxv];
struct edge {
  int to, next;
} e[maxn * 2];
int h[maxn], tot;
int dep[maxn], siz[maxn], son[maxn], top[maxn], fa[maxn];
void pre(int n = 1e7) {
  for (int i = 2; i <= n; ++i) {
    if (!d[i]) p[++pcnt] = i, d[i] = i;
    for (int j = 1; j <= pcnt && i * p[j] <= n; ++j) {
      d[i * p[j]] = p[j];
      if (i % p[j] == 0) break;
    }
  }
}
inline void add(int u, int v) {
  e[++tot] = (edge){v, h[u]};
  h[u] = tot;
  e[++tot] = (edge){u, h[v]};
  h[v] = tot;
}
void dfs1(int u) {
  siz[u] = 1;
  dep[u] = dep[fa[u]] + 1;
  for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
    if (v != fa[u]) {
      fa[v] = u;
      dfs1(v);
      siz[u] += siz[v];
      if (siz[v] > siz[son[u]]) son[u] = v;
    }
}
void dfs2(int u) {
  if (son[u]) top[son[u]] = top[u], dfs2(son[u]);
  for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
    if (v != fa[u] && v != son[u]) top[v] = v, dfs2(v);
}
int lca(int u, int v) {
  while (top[u] != top[v]) {
    if (dep[top[u]] > dep[top[v]])
      u = fa[top[u]];
    else
      v = fa[top[v]];
  }
  return dep[u] < dep[v] ? u : v;
}
inline int fpow(int x, int k) {
  int res = 1;
  while (k) {
    if (k & 1) res = (long long)res * x % mod;
    k >>= 1;
    x = (long long)x * x % mod;
  }
  return res;
}
inline void insert(int x, int v) {
  static int p, cnt;
  while (x > 1) {
    p = d[x];
    cnt = 0;
    while (d[x] == p) ++cnt, x /= d[x];
    for (int i = 0; i < cnt - 1; ++i) g[p][i] += (i + 1) * v;
    for (int i = cnt - 1; i < Log[p]; ++i) g[p][i] += cnt * v;
  }
}
inline int query(int x) {
  static int p, cnt;
  int res = 1;
  while (x > 1) {
    p = d[x];
    cnt = 0;
    while (d[x] == p) ++cnt, x /= d[x];
    res = (long long)res * fpow(p, g[p][cnt - 1]) % mod;
  }
  return res;
}
inline void dfs(int u) {
  insert(val[u], 1);
  for (auto t : Inc[u])
    ans[t.second] = (long long)ans[t.second] * query(t.first) % mod;
  for (auto t : Dec[u])
    ans[t.second] =
        (long long)ans[t.second] * fpow(query(t.first), mod - 2) % mod;
  for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
    if (v != fa[u]) dfs(v);
  insert(val[u], -1);
}
int main() {
  n = gi();
  for (int i = 1; i < n; ++i) add(gi(), gi());
  for (int i = 1; i <= n; ++i) val[i] = gi();
  pre();
  dfs1(1);
  top[1] = 1;
  dfs2(1);
  q = gi();
  for (int x, y, z, w, i = 1; i <= q; ++i) {
    x = gi();
    y = gi();
    z = lca(x, y);
    w = gi();
    Inc[x].push_back(make_pair(w, i));
    Inc[y].push_back(make_pair(w, i));
    Dec[z].push_back(make_pair(w, i));
    Dec[fa[z]].push_back(make_pair(w, i));
    ans[i] = 1;
  }
  int *now = tmp;
  for (int i = 1; i <= pcnt; ++i) {
    g[p[i]] = now;
    for (int j = 1; (long long)j * p[i] < maxv; j *= p[i]) ++Log[p[i]];
    now += Log[p[i]] + 1;
  }
  dfs(1);
  for (int i = 1; i <= q; ++i) printf("%d\n", ans[i]);
  return 0;
}