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 MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int MAXV = 1e7 + 5;
int n, q, qn, a[MAXN];
int hd[MAXN], nxt[MAXN << 1], to[MAXN << 1], ec = 0;
void adde(int u, int v) {
  to[++ec] = v;
  nxt[ec] = hd[u];
  hd[u] = ec;
}
int fa[MAXN][22], dep[MAXN];
void dfs(int x, int f) {
  fa[x][0] = f;
  for (int e = hd[x]; e; e = nxt[e]) {
    int y = to[e];
    if (y == f) continue;
    dep[y] = dep[x] + 1;
    dfs(y, x);
  }
}
int getlca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 20; ~i; i--)
    if (dep[x] - (1 << i) >= dep[y]) x = fa[x][i];
  if (x == y) return x;
  for (int i = 20; ~i; i--)
    if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}
vector<pair<int, int> > fac[MAXN];
struct Query {
  int w, x, id, mul;
} qu[MAXN << 2];
int qpow(int x, int e) {
  int ret = 1;
  while (e) {
    if (e & 1) ret = 1ll * ret * x % MOD;
    x = 1ll * x * x % MOD;
    e >>= 1;
  }
  return ret;
}
int ans[MAXN];
int sum[MAXV / 5][24];
vector<int> qv[MAXN];
int pid[MAXV], pr[MAXV / 5], pcnt = 0;
bitset<MAXV> vis;
void prework() {
  for (int i = 2; i < MAXV; i++) {
    if (!vis[i]) {
      pr[++pcnt] = i;
      pid[i] = pcnt;
    }
    for (int j = 1; j <= pcnt && pr[j] * i < MAXV; j++) {
      vis[pr[j] * i] = 1;
      if (i % pr[j] == 0) break;
    }
  }
}
void calc(int x) {
  for (__typeof(fac[x].begin()) it = fac[x].begin(); it != fac[x].end(); it++)
    sum[pid[it->first]][it->second]++;
  for (__typeof(qv[x].begin()) it = qv[x].begin(); it != qv[x].end(); it++) {
    int id = *it, tmp = qu[id].w;
    vector<pair<int, int> > ff;
    for (int i = 2; i * i <= tmp; i++) {
      if (tmp % i == 0) {
        int cnt = 0;
        while (tmp % i == 0) tmp /= i, cnt++;
        ff.push_back(make_pair(i, cnt));
      }
    }
    if (tmp > 1) ff.push_back(make_pair(tmp, 1));
    int tot_mul = 1;
    for (int i = 0; i < ff.size(); i++) {
      int pw = 1, val = ff[i].first;
      for (int j = 1; j <= ff[i].second; j++) pw *= ff[i].first;
      for (int j = 1; val < MAXV; j++, val *= ff[i].first) {
        int cnt = sum[pid[ff[i].first]][j];
        int mul = qpow(min(val, pw), cnt);
        tot_mul = 1ll * tot_mul * mul % MOD;
      }
    }
    if (qu[id].mul == 1)
      ans[qu[id].id] = 1ll * ans[qu[id].id] * tot_mul % MOD;
    else
      ans[qu[id].id] = 1ll * ans[qu[id].id] * qpow(tot_mul, MOD - 2) % MOD;
  }
  for (int e = hd[x]; e; e = nxt[e]) {
    int y = to[e];
    if (y == fa[x][0]) continue;
    calc(y);
  }
  for (__typeof(fac[x].begin()) it = fac[x].begin(); it != fac[x].end(); it++)
    sum[pid[it->first]][it->second]--;
}
int main() {
  prework();
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    adde(u, v);
    adde(v, u);
  }
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    int tmp = a[i];
    for (int j = 2; j * j <= tmp; j++) {
      if (tmp % j == 0) {
        int cnt = 0;
        while (tmp % j == 0) tmp /= j, cnt++;
        fac[i].push_back(make_pair(j, cnt));
      }
    }
    if (tmp > 1) fac[i].push_back(make_pair(tmp, 1));
  }
  dfs(1, 0);
  for (int i = 1; i <= 20; i++)
    for (int j = 1; j <= n; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1];
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    ans[i] = 1;
    int lca = getlca(u, v);
    qu[++qn] = {w, u, i, 1};
    qu[++qn] = {w, v, i, 1};
    qu[++qn] = {w, lca, i, -1};
    if (lca != 1) qu[++qn] = {w, fa[lca][0], i, -1};
  }
  for (int i = 1; i <= qn; i++) qv[qu[i].x].push_back(i);
  calc(1);
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}