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 oo = 1e9;
const int LOGN = 18;
const int MAX_VAL = 10000000;
const int MAX_N = 200000;
const int MOD = 1000000007;
int sm[MAX_VAL + 1], a[MAX_N + 1], n, q, in[MAX_N + 10], out[MAX_N + 10], cnt,
    BIT[MAX_N + 10], dp[MAX_N + 10][LOGN], depth[MAX_N + 1];
long long res[MAX_N + 1];
vector<int> g[MAX_N + 1];
vector<pair<pair<int, int>, int> > pri;
multiset<pair<int, int> > mst;
struct query {
  int u, v, first, idx;
  query() {}
};
query cr[MAX_N + 1];
map<int, deque<pair<int, int> > > num;
vector<pair<int, int> > rev;
int last = -1, curFreq = -1;
void fact(int val, int idx, bool type) {
  map<int, int> fr;
  while (val > 1) {
    fr[sm[val]]++;
    val /= sm[val];
  }
  for (auto it : fr) {
    if (type) {
      pri.push_back(make_pair(make_pair(it.first, it.second), idx));
    } else {
      num[it.first].push_back(make_pair(it.second, idx));
    }
  }
}
void addFen(int idx, int val) {
  while (idx <= cnt) {
    BIT[idx] += val;
    idx += (idx & -idx);
  }
}
int getFen(int idx) {
  int res = 0;
  while (idx) {
    res += BIT[idx];
    idx -= (idx & -idx);
  }
  return res;
}
void dfs(int u, int p) {
  dp[u][0] = p;
  in[u] = ++cnt;
  for (int i = 0; i < g[u].size(); ++i) {
    int v = g[u][i];
    if (v == p) continue;
    depth[v] = depth[u] + 1;
    dfs(v, u);
  }
  out[u] = ++cnt;
}
void buildLCA() {
  for (int k = 1; k < LOGN; ++k) {
    for (int u = 1; u <= n; ++u) {
      if (dp[u][k - 1] == -1) continue;
      dp[u][k] = dp[dp[u][k - 1]][k - 1];
    }
  }
}
int lca(int u, int v) {
  if (depth[v] > depth[u]) swap(u, v);
  for (int k = LOGN - 1; k >= 0; --k) {
    if (depth[u] - (1 << k) >= depth[v]) {
      u = dp[u][k];
    }
  }
  if (u == v) return u;
  for (int k = LOGN - 1; k >= 0; --k) {
    if (dp[u][k] != dp[v][k]) {
      u = dp[u][k];
      v = dp[v][k];
    }
  }
  return dp[u][0];
}
long long fast(int a, int b) {
  if (!b) return 1ll;
  long long res = fast(a, b / 2);
  res = (res * res) % MOD;
  if (b & 1) return (res * a) % MOD;
  return res;
}
void addtoNode(int u) {
  addFen(in[u], 1);
  addFen(out[u], -1);
  rev.push_back(make_pair(in[u], -1));
  rev.push_back(make_pair(out[u], +1));
}
int getSum(int u) { return getFen(in[u]); }
int main() {
  memset(sm, -1, sizeof sm);
  memset(dp, -1, sizeof dp);
  for (int i = 2; i <= MAX_VAL; ++i) {
    if (sm[i] != -1) continue;
    for (int j = i; j <= MAX_VAL; j += i) sm[j] = i;
  }
  cin >> n;
  for (int u, v, i = 0; i < n - 1; ++i) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v), g[v].push_back(u);
  }
  dfs(1, -1);
  buildLCA();
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    fact(a[i], i, 0);
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    scanf("%d%d%d", &cr[i].u, &cr[i].v, &cr[i].first);
    cr[i].idx = i;
    res[i] = 1;
    fact(cr[i].first, i, 1);
  }
  sort(pri.begin(), pri.end());
  for (int i = 0; i < pri.size(); ++i) {
    if (last != pri[i].first.first) {
      curFreq = 0;
      last = pri[i].first.first;
      while (rev.size()) {
        pair<int, int> bk = rev.back();
        addFen(bk.first, bk.second);
        rev.pop_back();
      }
    }
    while (curFreq < pri[i].first.second) {
      deque<pair<int, int> > nw;
      deque<pair<int, int> > &at = num[pri[i].first.first];
      for (int j = 0; j < at.size(); ++j) {
        addtoNode(at[j].second);
        at[j].first--;
        if (at[j].first) nw.push_back(at[j]);
      }
      at = nw;
      curFreq++;
    }
    query &f = cr[pri[i].second];
    int lc = lca(f.u, f.v);
    int pw = getSum(f.u) + getSum(f.v) - getSum(lc) -
             (dp[lc][0] == -1 ? 0 : getSum(dp[lc][0]));
    res[pri[i].second] =
        (1ll * res[pri[i].second] * fast(pri[i].first.first, pw)) % MOD;
  }
  for (int i = 0; i < q; ++i) {
    printf("%lld\n", res[i]);
  }
  return 0;
}