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 N = 1e5 + 10, mo = 1e9 + 7;
const int S = 1e7 + 10;
int n, q, final[N], nex[2 * N], to[2 * N], tot, dfn[N], dfnr[N], stm, a[N];
int ans[N];
int qfinal[S], qnex[8 * N], qto[8 * N], qtot, zs[8 * N];
int ffinal[S], fnex[8 * N], fto[8 * N], ftot, fzs[8 * N];
int g[N][20], dep[N];
struct qry {
  int u, v, no, lca, w;
} qu[N];
bool cmp(qry a, qry b) { return a.w < b.w; }
void link(int x, int y) { to[++tot] = y, nex[tot] = final[x], final[x] = tot; }
int p[1000000], mf[S];
bool bz[S];
void init() {
  for (int i = 2; i < S; i++) {
    if (!bz[i]) p[++p[0]] = i, mf[i] = i;
    for (int j = 1; j <= p[0] && p[j] * i < S; j++) {
      mf[i * p[j]] = p[j];
      bz[i * p[j]] = 1;
      if (i % p[j] == 0) break;
    }
  }
}
void dfs(int x) {
  dfn[x] = ++stm;
  dep[x] = dep[g[x][0]] + 1;
  for (int i = 1; i <= 17; i++) g[x][i] = g[g[x][i - 1]][i - 1];
  for (int i = final[x]; i; i = nex[i]) {
    int y = to[i];
    if (y != g[x][0]) g[y][0] = x, dfs(y);
  }
  dfnr[x] = stm;
}
int getlca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 17; i >= 0; i--)
    if (dep[g[x][i]] >= dep[y]) x = g[x][i];
  if (x == y) return y;
  for (int i = 17; i >= 0; i--)
    if (g[x][i] != g[y][i]) x = g[x][i], y = g[y][i];
  return g[x][0];
}
qry L[N + N];
int r;
int zc[N], sum[N];
int sumt[N], szt[N];
int ksm(int x, int y) {
  long long ret = 1;
  for (; y; y >>= 1) {
    if (y & 1) ret = ret * x % mo;
    x = (long long)x * x % mo;
  }
  return ret;
}
void change(int x, int v) {
  int sig = (v > 0) ? -1 : 1;
  for (; x <= n + 1; x += ((x) & -(x))) sumt[x] += v, szt[x] += sig;
}
void change1(int x, int v) {
  for (; x <= n + 1; x += ((x) & -(x))) szt[x] += v;
}
void change2(int x, int v) {
  for (; x <= n + 1; x += ((x) & -(x))) sumt[x] += v;
}
void query(int x, int &s, int &t, int xs) {
  for (; x; x -= ((x) & -(x))) s += sumt[x] * xs, t += szt[x] * xs;
}
void solve(int p) {
  r = 0;
  for (int i = qfinal[p]; i; i = qnex[i]) {
    L[++r] = qu[qto[i]];
    L[r].w = zs[i];
  }
  for (int i = ffinal[p]; i; i = fnex[i]) {
    L[++r].u = fto[i];
    L[r].w = fzs[i];
    L[r].no = 0;
    zc[L[r].u] = 1;
    change1(dfn[L[r].u], 1);
    change1(dfnr[L[r].u] + 1, -1);
  }
  sort(L + 1, L + 1 + r, cmp);
  for (int i = 1; i <= r; i++) {
    if (L[i].no == 0) {
      sum[L[i].u] += L[i].w;
      zc[L[i].u] = 0;
      change(dfn[L[i].u], L[i].w);
      change(dfnr[L[i].u] + 1, -L[i].w);
    } else {
      int cnt = 0, df = 0;
      query(dfn[L[i].u], df, cnt, 1);
      query(dfn[L[i].v], df, cnt, 1);
      query(dfn[L[i].lca], df, cnt, -2);
      df += (zc[L[i].lca] + cnt) * L[i].w;
      df += sum[L[i].lca];
      ans[L[i].no] = ((long long)ans[L[i].no] * ksm(p, df)) % mo;
    }
  }
  for (int i = 1; i <= r; i++) {
    if (L[i].no == 0) {
      change2(dfn[L[i].u], -L[i].w);
      change2(dfnr[L[i].u] + 1, +L[i].w);
      sum[L[i].u] = 0;
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    link(u, v), link(v, u);
  }
  dfs(1);
  init();
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    int z = a[i];
    while (z != 1) {
      int k = mf[z], cnt = 0;
      while (z % k == 0) cnt++, z /= k;
      fzs[++ftot] = cnt;
      fto[ftot] = i;
      fnex[ftot] = ffinal[k];
      ffinal[k] = ftot;
    }
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int z;
    scanf("%d %d %d\n", &qu[i].u, &qu[i].v, &z);
    qu[i].lca = getlca(qu[i].u, qu[i].v);
    qu[i].no = i;
    ans[i] = 1;
    while (z != 1) {
      int k = mf[z], cnt = 0;
      while (z % k == 0) cnt++, z /= k;
      zs[++qtot] = cnt;
      qto[qtot] = i;
      qnex[qtot] = qfinal[k];
      qfinal[k] = qtot;
    }
  }
  for (int i = 1; i <= p[0]; i++)
    if (ffinal[p[i]] && qfinal[p[i]] != 0) solve(p[i]);
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
}