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 MAXBUF = 1 << 23;
char B[MAXBUF], *Si = B, *Ti = B;
inline char getc() {
  if (Si == Ti) Ti = (Si = B) + fread(B, 1, MAXBUF, stdin);
  if (Si == Ti)
    return 0;
  else
    return *Si++;
}
template <class T>
inline void read(T &a) {
  static char c;
  static int fh;
  while (((c = getc()) < '0' || c > '9') && c != '-')
    ;
  if (c == '-')
    fh = -1, a = 0;
  else
    fh = 1, a = c - '0';
  while ((c = getc()) <= '9' && c >= '0') a = (a << 3) + (a << 1) + c - '0';
  if (fh == -1) a = -a;
}
char Buff[MAXBUF], *sti = Buff;
template <class T>
inline void write(T a) {
  if (a == 0) {
    *sti++ = '0';
    return;
  }
  if (a < 0) *sti++ = '-', a = -a;
  static char c[20];
  static int c0;
  c0 = 0;
  while (a) c[c0++] = a % 10 + '0', a /= 10;
  while (c0--) *sti++ = c[c0];
}
inline int qpow(int a, int b) {
  int res = 1, tp = a;
  while (b) {
    if (b & 1) res = 1ll * res * tp % 1000000007;
    tp = 1ll * tp * tp % 1000000007;
    b >>= 1;
  }
  return res;
}
int head[100005], nxt[200005], v[200005], w[200005], tot = 0;
int id[200005], l[100005], r[100005], ans[100005], n, q;
int top[100005], fa[100005], son[100005], siz[100005];
int fla[10000005], pri[1000005], dep[100005], cnt = 0;
int sz[100005], t1[10000005], t2[4005][35];
bool vis[100005], isp[10000005];
pair<int, int> p[100005][11];
int lp, rp, LM = 4000;
struct node {
  int bl, l, r, id, x, tp;
} o[100005];
inline bool cmp(node a, node b) {
  if (a.bl != b.bl) return a.bl < b.bl;
  if (a.bl & 1) return a.r > b.r;
  return a.r < b.r;
}
inline void add(int a, int b) {
  tot++;
  nxt[tot] = head[a];
  head[a] = tot;
  v[tot] = b;
}
inline void getp() {
  for (int i = 2; i <= 10000000; i++) {
    if (!isp[i]) {
      fla[i] = i;
      pri[++cnt] = i;
    }
    for (int j = 1; j <= cnt && i * pri[j] <= 10000000; j++) {
      fla[i * pri[j]] = pri[j];
      isp[i * pri[j]] = 1;
      if (i % pri[j] == 0) break;
    }
  }
}
void dfs(int pos, int f) {
  l[pos] = ++cnt;
  id[cnt] = pos;
  siz[pos] = 1;
  for (int i = head[pos]; i; i = nxt[i]) {
    if (v[i] == f) continue;
    dep[v[i]] = dep[pos] + 1;
    fa[v[i]] = pos;
    dfs(v[i], pos);
    siz[pos] += siz[v[i]];
    if (siz[v[i]] > siz[son[pos]]) son[pos] = v[i];
  }
  r[pos] = ++cnt;
  id[cnt] = pos;
}
void dfs2(int pos, int tp) {
  top[pos] = tp;
  if (son[pos]) dfs2(son[pos], tp);
  for (int i = head[pos]; i; i = nxt[i]) {
    if (v[i] == fa[pos]) continue;
    if (v[i] == son[pos]) continue;
    dfs2(v[i], v[i]);
  }
}
inline int lca(int a, int b) {
  while (top[a] ^ top[b]) {
    if (dep[top[a]] < dep[top[b]]) swap(a, b);
    a = fa[top[a]];
  }
  return (dep[a] < dep[b] ? a : b);
}
inline void work1(int o) {
  for (int i = 1; i <= sz[o]; i++) {
    if (p[o][i].first <= LM) {
      t2[p[o][i].first][p[o][i].second]--;
    } else {
      t1[p[o][i].first]--;
    }
  }
}
inline void work2(int o) {
  for (int i = 1; i <= sz[o]; i++) {
    if (p[o][i].first <= LM) {
      t2[p[o][i].first][p[o][i].second]++;
    } else {
      t1[p[o][i].first]++;
    }
  }
}
inline void add(int o) {
  if (vis[o]) {
    vis[o] ^= 1;
    work1(o);
  } else {
    vis[o] ^= 1;
    work2(o);
  }
}
int main() {
  int i, j, a, b, c;
  getp();
  read(n);
  for (i = 1; i < n; i++) {
    read(a);
    read(b);
    add(a, b);
    add(b, a);
  }
  cnt = 0;
  dfs(1, 0);
  dfs2(1, 1);
  for (i = 1; i <= n; i++) {
    read(a);
    while (a != 1) {
      int pii = fla[a], o = 0;
      while (a % pii == 0) {
        a /= pii;
        o++;
      }
      p[i][++sz[i]] = make_pair(pii, o);
    }
  }
  read(q);
  for (i = 1; i <= q; i++) {
    read(o[i].l);
    read(o[i].r);
    o[i].tp = lca(o[i].l, o[i].r);
    o[i].l = l[o[i].l];
    o[i].r = l[o[i].r];
    if (o[i].l > o[i].r) swap(o[i].l, o[i].r);
    o[i].l++;
    read(o[i].x);
    o[i].id = i;
    o[i].bl = (o[i].l >> 9);
  }
  sort(o + 1, o + q + 1, cmp);
  lp = 1;
  rp = 1;
  add(id[1]);
  for (i = 1; i <= q; i++) {
    while (lp > o[i].l) add(id[--lp]);
    while (rp < o[i].r) add(id[++rp]);
    while (lp < o[i].l) add(id[lp++]);
    while (rp > o[i].r) add(id[rp--]);
    add(o[i].tp);
    a = o[i].x;
    int res = 1;
    while (a != 1) {
      int p = fla[a], o = 0;
      while (a % p == 0) {
        a /= p;
        o++;
      }
      if (p > LM) {
        res = 1ll * res * qpow(p, t1[p]) % 1000000007;
        continue;
      }
      int tp = 1, u = 0;
      for (j = 1; j < o; j++) {
        tp = 1ll * tp * p % 1000000007;
        if (!t2[p][j]) continue;
        res = 1ll * res * qpow(tp, t2[p][j]) % 1000000007;
      }
      tp = 1ll * tp * p % 1000000007;
      for (j = o; j <= 30; j++) u += t2[p][j];
      res = 1ll * res * qpow(tp, u) % 1000000007;
    }
    ans[o[i].id] = res;
    add(o[i].tp);
  }
  for (i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}