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 long long maxn = 1e5 + 10;
const long long mod = 1e9 + 7;
const long long base = 1e18;
vector<long long> adj[maxn];
long long dep[maxn];
long long anc[maxn][20];
long long a[maxn];
int p[100 * maxn];
long long u[maxn];
long long v[maxn];
long long x[maxn];
long long siz[maxn];
void dfs(long long u, long long par) {
  anc[u][0] = par;
  for (int i = 1; i <= 19; i++) {
    anc[u][i] = anc[anc[u][i - 1]][i - 1];
  }
  siz[u] = 1;
  for (auto to : adj[u]) {
    if (to == par) continue;
    dep[to] = dep[u] + 1;
    dfs(to, u);
    siz[u] += siz[to];
  }
}
long long lca(long long x, long long y) {
  if (dep[x] < dep[y]) swap(x, y);
  long long kc = dep[x] - dep[y];
  for (int i = 19; i >= 0; i--) {
    if (kc & (1ll << i)) {
      x = anc[x][i];
    }
  }
  if (x == y) return x;
  for (int i = 19; i >= 0; i--) {
    if (anc[x][i] != anc[y][i]) {
      x = anc[x][i];
      y = anc[y][i];
    }
  }
  return anc[x][0];
}
long long cntnw = 0;
long long nchain = 1;
long long chainhead[maxn];
long long chainid[maxn];
long long id[maxn];
void hld(long long u, long long par) {
  if (!chainhead[nchain]) chainhead[nchain] = u;
  cntnw++;
  chainid[u] = nchain;
  id[u] = cntnw;
  long long nxt = -1;
  for (auto to : adj[u]) {
    if (to == par) continue;
    if (nxt == -1 || siz[nxt] < siz[to]) {
      nxt = to;
    }
  }
  if (nxt != -1) {
    hld(nxt, u);
  }
  for (auto to : adj[u]) {
    if (to == par || to == nxt) continue;
    nchain++;
    hld(to, u);
  }
}
long long n;
long long fwt[2 * maxn];
vector<long long> vt;
void update(long long id, long long diff) {
  vt.push_back(id);
  for (int i = id; i <= n; i += (i & (-i))) {
    fwt[i] += diff;
  }
}
long long get(long long l, long long r) {
  long long ans = 0;
  for (int i = r; i; i -= (i & (-i))) {
    ans += fwt[i];
  }
  for (int i = l - 1; i; i -= (i & (-i))) {
    ans -= fwt[i];
  }
  return ans;
}
long long get1(long long u, long long a) {
  long long p = chainid[u];
  long long chk = chainid[a];
  long long ans = 0;
  while (1) {
    if (p == chk) {
      ans += get(id[a], id[u]);
      break;
    }
    ans += get(id[chainhead[p]], id[u]);
    u = anc[chainhead[p]][0];
    p = chainid[u];
  }
  return ans;
}
long long mu(long long a, long long n) {
  if (n == 0) return 1;
  if (n == 1) return a;
  long long t = mu(a, n / 2);
  if (n % 2 == 0) return (t * t) % mod;
  return ((t * t) % mod * a) % mod;
}
vector<pair<long long, long long> > gr[10 * maxn];
vector<pair<long long, long long> > gr1[10 * maxn];
long long ans[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen("t.inp", "r")) {
    freopen("test.inp", "r", stdin);
    freopen("test.out", "w", stdout);
  }
  long long th = 1e7;
  vector<long long> vt2;
  for (int i = 2; i <= th; i++) {
    if (p[i]) continue;
    vt2.push_back(i);
    for (int j = i; j <= th; j += i) {
      p[j] = i;
    }
  }
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    long long x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  dfs(1, 0);
  hld(1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    long long t = a[i];
    while (t != 1) {
      long long h = p[t];
      long long cnt = 0;
      while (t % h == 0) {
        cnt++;
        t /= h;
      }
      h = lower_bound(vt2.begin(), vt2.end(), h) - vt2.begin();
      gr1[h].push_back(make_pair(cnt, i));
    }
  }
  long long q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> u[i] >> v[i] >> x[i];
    ans[i] = 1;
    long long t = x[i];
    while (t != 1) {
      long long h = p[t];
      long long cnt = 0;
      while (t % h == 0) {
        cnt++;
        t /= h;
      }
      h = lower_bound(vt2.begin(), vt2.end(), h) - vt2.begin();
      gr[h].push_back(make_pair(cnt, i));
    }
  }
  for (int i = 0; i <= vt2.size() - 1; i++) {
    if (!gr[i].size()) continue;
    sort(gr[i].begin(), gr[i].end());
    long long j = 0;
    for (int bit = 1; bit < 30; bit++) {
      for (auto to : gr1[i]) {
        if (to.first < bit) continue;
        long long h = id[to.second];
        update(h, 1);
      }
      while (j < gr[i].size() && gr[i][j].first == bit) {
        long long id1 = gr[i][j].second;
        long long h = lca(u[id1], v[id1]);
        long long cnt = get1(u[id1], h) + get1(v[id1], h) - get(id[h], id[h]);
        ans[id1] = (ans[id1] * mu(vt2[i], cnt)) % mod;
        j++;
      }
    }
    auto vt1 = vt;
    for (auto p : vt1) {
      update(p, -1);
    }
    vt.clear();
  }
  for (int i = 1; i <= q; i++) {
    cout << ans[i] << "\n";
  }
}