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 void read(int &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
int i, j, k, m, n, s, t, dfxn;
const int N = 7e5 + 10, mod = 1e9 + 7;
struct q {
  int u, v, w, rk;
};
struct q2 {
  int first, second;
};
int p[10000010];
int zs[N], head[N], son[N], size[N], dfn[N], rak[N];
int fa[N], ans[N], c[N][29], dep[N], top[N];
vector<q2> vct[N];
vector<q> vctt[N];
struct fu {
  int v, nxt;
} a[N << 1];
void work(int aa, int bb) {
  a[++k].nxt = head[aa];
  head[aa] = k;
  a[k].v = bb;
}
void init() {
  dep[1] = 1;
  for (i = 2; i <= 1e7; i++) {
    if (p[i] != 1) zs[++t] = i, p[i] = t;
    for (j = 1; j <= t && zs[j] * i <= 1e7; j++) {
      p[i * zs[j]] = 1;
      if (i % zs[j] == 0) break;
    }
  }
}
void dfs(int u) {
  for (int i = head[u]; i; i = a[i].nxt) {
    int v = a[i].v;
    if (fa[u] == v) continue;
    dep[v] = dep[u] + 1;
    fa[v] = u;
    dfs(v);
    size[u] += size[v];
    if (size[v] > size[son[u]]) son[u] = v;
  }
  size[u]++;
}
void dfs2(int u) {
  dfn[u] = ++dfxn;
  rak[dfn[u]] = u;
  if (son[fa[u]] == u)
    top[u] = top[fa[u]];
  else
    top[u] = u;
  if (son[u]) dfs2(son[u]);
  for (int i = head[u]; i; i = a[i].nxt) {
    int v = a[i].v;
    if (v == son[u] || v == fa[u]) continue;
    dfs2(v);
  }
}
void fj(int x, int u) {
  int tmp = sqrt(x + 1);
  for (int i = 1; zs[i] <= tmp; i++) {
    int cnt = 0;
    while (x % zs[i] == 0) {
      cnt++;
      x /= zs[i];
    }
    if (cnt) vct[i].push_back((q2){u, cnt});
  }
  if (x != 1) vct[p[x]].push_back((q2){u, 1});
}
int quick(int aa, int ss) {
  int ret = 1;
  while (ss) {
    if (ss & 1) ret = (long long)ret * aa % mod;
    aa = (long long)aa * aa % mod;
    ss >>= 1;
  }
  return ret;
}
void fj2(int x, int u, int v, int rk) {
  int tmp = sqrt(x + 1);
  for (int i = 1; zs[i] <= tmp; i++) {
    int cnt = 0;
    while (x % zs[i] == 0) {
      cnt++;
      x /= zs[i];
    }
    if (cnt) vctt[i].push_back((q){u, v, cnt, rk});
  }
  if (x != 1) vctt[p[x]].push_back((q){u, v, 1, rk});
}
void updata(int x, int pp, int cc) {
  for (int i = x; i <= n; i += i & (-i)) c[i][pp] += cc;
}
int quert(int x, int tc, int w) {
  int ret = 0, ss;
  for (int j = 1; j <= tc; j++) {
    ss = 0;
    for (int i = x; i >= 1; i -= i & (-i)) ss += c[i][j];
    ret += ss * min(w, j);
  }
  return ret;
}
int query(int u, int v, int tc, int w) {
  if (dep[u] < dep[v]) swap(u, v);
  if (top[u] == top[v]) return quert(dfn[u], tc, w) - quert(dfn[v] - 1, tc, w);
  if (dep[top[u]] < dep[top[v]]) swap(u, v);
  return quert(dfn[u], tc, w) - quert(dfn[top[u]] - 1, tc, w) +
         query(fa[top[u]], v, tc, w);
}
int main() {
  init();
  read(n);
  for (i = 1; i < n; i++) {
    int x, y;
    read(x), read(y);
    work(x, y);
    work(y, x);
  }
  dfs(1);
  dfs2(1);
  for (i = 1; i <= n; i++) {
    int x;
    read(x);
    fj(x, i);
  }
  read(m);
  for (i = 1; i <= m; i++) {
    int x, y, w;
    read(x), read(y), read(w);
    ans[i] = 1;
    fj2(w, x, y, i);
  }
  for (i = 1; i <= t; i++) {
    for (j = 0; j < vct[i].size(); j++)
      updata(dfn[vct[i][j].first], vct[i][j].second, 1);
    for (j = 0; j < vctt[i].size(); j++) {
      int rc = log2(1e7) / log2(zs[i]) + 1;
      int tmp = (long long)query(vctt[i][j].u, vctt[i][j].v, rc, vctt[i][j].w);
      ans[vctt[i][j].rk] =
          (long long)ans[vctt[i][j].rk] * quick(zs[i], tmp) % mod;
    }
    for (j = 0; j < vct[i].size(); j++)
      updata(dfn[vct[i][j].first], vct[i][j].second, -1);
  }
  for (i = 1; i <= m; i++) printf("%d ", ans[i]);
  return 0;
}