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>
const int maxn = 100010, MAX = 10000000, maxp = 666666, maxq = 100010,
          mod = 1000000007;
int n, q, qu[maxq], qv[maxq], qx[maxq], qans[maxq];
struct edge {
  int to;
  edge* next;
} E[maxn * 2], *ne = E, *fir[maxn];
void link(int u, int v) {
  *ne = (edge){v, fir[u]};
  fir[u] = ne++;
}
int pr[maxp], pc, mf[MAX + 1];
std::vector<int> st_p[maxp], st_c[maxp], qr_p[maxp], qr_c[maxp];
int pw(int a, int n) {
  int b = 1;
  for (; n; n >>= 1) n % 2 ? b = 1ll * b * a % mod : 1, a = 1ll * a * a % mod;
  return b;
}
std::vector<int> tmp_st[33], tmp_qr[33];
int fa[maxn], dep[maxn], siz[maxn], son[maxn], top[maxn], pre[maxn], pos[maxn],
    now;
void init(int i) {
  siz[i] = 1;
  pre[i] = now++;
  for (edge* e = fir[i]; e; e = e->next)
    if (e->to != fa[i]) {
      fa[e->to] = i;
      dep[e->to] = dep[i] + 1;
      init(e->to);
      if (siz[e->to] > siz[son[i]]) son[i] = e->to;
      siz[i] += siz[e->to];
    }
  pos[i] = now;
}
void hld(int i) {
  for (edge* e = fir[i]; e; e = e->next)
    if (e->to != fa[i]) {
      top[e->to] = e->to == son[i] ? top[i] : e->to;
      hld(e->to);
    }
}
int BIT[2][maxn];
void add(int t, int i, int x) {
  for (; i <= n; i += i & -i) BIT[t][i] += x;
}
int qry(int t, int i) {
  int s = 0;
  for (; i; i -= i & -i) s += BIT[t][i];
  return s;
}
int lca(int i, int j) {
  while (top[i] != top[j]) {
    if (dep[top[i]] > dep[top[j]])
      i = fa[top[i]];
    else
      j = fa[top[j]];
  }
  return dep[i] < dep[j] ? i : j;
}
int main() {
  for (int i = 2; i <= MAX; i++) {
    if (!mf[i]) pr[++pc] = i, mf[i] = pc;
    for (int j = 1; j <= pc && i * pr[j] <= MAX; j++) {
      mf[i * pr[j]] = j;
      if (i % pr[j] == 0) break;
    }
  }
  scanf("%d", &n);
  for (int i = 1, u, v; i < n; i++) {
    scanf("%d%d", &u, &v);
    link(u, v);
    link(v, u);
  }
  init(1);
  top[1] = 1;
  hld(1);
  for (int i = 1, a; i <= n; i++) {
    scanf("%d", &a);
    while (a > 1) {
      int p = mf[a], c = 0;
      while (a % pr[p] == 0) a /= pr[p], c++;
      st_p[p].push_back(i);
      st_c[p].push_back(c);
    }
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d", qu + i, qv + i, qx + i);
    int x = qx[i];
    while (x > 1) {
      int p = mf[x], c = 0;
      while (x % pr[p] == 0) x /= pr[p], c++;
      qr_p[p].push_back(i);
      qr_c[p].push_back(c);
    }
    qans[i] = 1;
  }
  for (int i = 1; i <= pc; i++) {
    int sc = st_p[i].size(), qc = qr_p[i].size(), mxc = 0;
    for (int j = 0; j < sc; j++)
      if (st_c[i][j] > mxc) mxc = st_c[i][j];
    for (int j = 0; j < qc; j++)
      if (qr_c[i][j] > mxc) mxc = qr_c[i][j];
    for (int j = 0; j <= mxc; j++) {
      tmp_st[j] = std::vector<int>();
      tmp_qr[j] = std::vector<int>();
    }
    for (int j = 0; j < sc; j++) {
      tmp_st[st_c[i][j]].push_back(st_p[i][j]);
      add(1, pre[st_p[i][j]] + 1, 1);
      add(1, pos[st_p[i][j]] + 1, -1);
    }
    for (int j = 0; j < qc; j++) tmp_qr[qr_c[i][j]].push_back(qr_p[i][j]);
    for (int j = 1; j <= mxc; j++) {
      for (int k = tmp_qr[j].size(); k--;) {
        int qid = tmp_qr[j][k], u = qu[qid], v = qv[qid], s = 0;
        s += qry(0, pre[u] + 1) + qry(1, pre[u] + 1) * j;
        s += qry(0, pre[v] + 1) + qry(1, pre[v] + 1) * j;
        int a = lca(u, v);
        s -= qry(0, pre[a] + 1) + qry(1, pre[a] + 1) * j;
        if (fa[a]) s -= qry(0, pre[fa[a]] + 1) + qry(1, pre[fa[a]] + 1) * j;
        qans[qid] = 1ll * qans[qid] * pw(pr[i], s) % mod;
      }
      for (int k = tmp_st[j].size(); k--;) {
        add(0, pre[tmp_st[j][k]] + 1, j);
        add(0, pos[tmp_st[j][k]] + 1, -j);
        add(1, pre[tmp_st[j][k]] + 1, -1);
        add(1, pos[tmp_st[j][k]] + 1, 1);
      }
    }
    for (int j = 0; j < sc; j++) {
      add(0, pre[st_p[i][j]] + 1, -st_c[i][j]);
      add(0, pos[st_p[i][j]] + 1, st_c[i][j]);
    }
  }
  for (int i = 0; i < q; i++) printf("%d\n", qans[i]);
}