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;
int n, x, y, z, q;
bool com[10000002];
int sm[10000002], fr[10000002], a[1000005], ans[1000005], baba[17 + 2][1000005],
    der[1000005];
vector<int> v[1000005];
vector<pair<pair<int, int>, int> > qu[1000005];
int mul(int x, int y) {
  int res = 1ll * x * y % 1000000007;
  while (res < 0) res += 1000000007;
  while (res >= 1000000007) res -= 1000000007;
  return res;
}
int fe(int x, int y) {
  if (y == 0) return 1;
  if (y == 1) return x;
  int res = fe(x, y / 2);
  res = mul(res, res);
  if (y & 1) res = mul(res, x);
  return res;
}
void add(int val, int ml) {
  while (val > 1) {
    int sp = sm[val];
    int cnt = 0;
    int po = 1;
    while (sm[val] == sp) {
      val /= sp;
      cnt++;
      po *= sp;
    }
    for (int i = cnt; i >= 0; i--, po /= sp) fr[po] += ml;
  }
}
void solve(int node, int ata) {
  add(a[node], 1);
  for (pair<pair<int, int>, int> x : qu[node]) {
    while (x.first.first > 1) {
      int sp = sm[x.first.first];
      int crp = (x.first.second == 1 ? sp : fe(sp, 1000000007 - 2));
      int inc = fe(crp, 1000000007 - 2);
      int cnt = 0;
      int po1 = 1;
      int po2 = 1;
      while (sm[x.first.first] == sp) {
        x.first.first /= sp;
        cnt++;
        po1 = mul(po1, crp);
        po2 = mul(po2, sp);
      }
      int totdec = 0;
      for (int i = cnt; i >= 0; i--) {
        ans[x.second] = mul(ans[x.second], fe(po1, fr[po2] - totdec));
        totdec += (fr[po2] - totdec);
        po2 /= sp;
        po1 = mul(po1, inc);
      }
    }
  }
  for (int i : v[node]) {
    if (i == ata) continue;
    solve(i, node);
  }
  add(a[node], -1);
}
void sieve() {
  for (int i = 2; i < 10000002; i++) {
    if (com[i]) continue;
    for (int j = i; j < 10000002; j += i) {
      if (!sm[j]) sm[j] = i;
      com[j] = 1;
    }
  }
}
int getlca(int x, int y) {
  if (der[x] < der[y]) swap(x, y);
  int dx = der[x];
  int _p = (1 << (17));
  for (int i = 17; i >= 0; i--, _p /= 2) {
    if (dx - _p >= der[y]) {
      dx -= _p;
      x = baba[i][x];
    }
  }
  if (x == y) return x;
  for (int i = 17; i >= 0; i--) {
    if (baba[i][x] != baba[i][y]) {
      x = baba[i][x];
      y = baba[i][y];
    }
  }
  return baba[0][x];
}
void buildlca() {
  for (int i = 1; i <= 17; i++) {
    for (int j = 1; j <= n; j++) {
      baba[i][j] = baba[i - 1][baba[i - 1][j]];
    }
  }
}
void dfs(int node, int ata) {
  baba[0][node] = ata;
  der[node] = der[ata] + 1;
  for (int i : v[node]) {
    if (i == ata) continue;
    dfs(i, node);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  dfs(1, 0);
  buildlca();
  sieve();
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d %d %d", &x, &y, &z);
    int lca = getlca(x, y);
    qu[x].push_back({{z, 1}, i});
    qu[y].push_back({{z, 1}, i});
    qu[lca].push_back({{z, -1}, i});
    qu[baba[0][lca]].push_back({{z, -1}, i});
  }
  fill(ans + 1, ans + 1 + n, 1);
  solve(1, 0);
  for (int i = 1; i <= q; i++) {
    printf("%d\n", ans[i]);
  }
}