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 M = 1e7;
const int N = 1e5 + 5;
const long long mod = 1e9 + 7;
int MX;
vector<int> sum, cnt, L, R, adj[N], PR;
vector<pair<int, int> > fact[N];
set<int> st;
int P[N][20], Lvl[N], a[N], root[N], n;
int lp[M + 1], primeLeft[M + 1];
int copy(int i = -1) {
  sum.push_back(i < 0 ? 0 : sum[i]);
  cnt.push_back(i < 0 ? 0 : cnt[i]);
  L.push_back(i < 0 ? 0 : L[i]);
  R.push_back(i < 0 ? 0 : R[i]);
  return (int)L.size() - 1;
}
int update(int p, int index, int val, int l = 0, int r = MX - 1) {
  p = copy(p);
  sum[p] += val;
  cnt[p]++;
  if (l == r) return p;
  int mid = (l + r) >> 1;
  if (index <= mid) {
    int temp = update(L[p], index, val, l, mid);
    L[p] = temp;
  } else {
    int temp = update(R[p], index, val, mid + 1, r);
    R[p] = temp;
  }
  return p;
}
pair<int, int> query(int p, int a, int b, int l = 0, int r = MX - 1) {
  if (b < l | r < a) return {0, 0};
  if (a <= l && r <= b) return {sum[p], cnt[p]};
  int mid = (l + r) >> 1;
  pair<int, int> left = query(L[p], a, b, l, mid);
  pair<int, int> right = query(R[p], a, b, mid + 1, r);
  return {left.first + right.first, left.second + right.second};
}
vector<pair<int, int> > getPrimeFact(int val) {
  vector<pair<int, int> > ret;
  for (int i = 0; i < PR.size() && PR[i] <= val / PR[i]; i++)
    if (val % PR[i] == 0) {
      pair<int, int> x = {PR[i], 0};
      while (val % PR[i] == 0) val /= PR[i], x.second++;
      ret.push_back(x);
    }
  if (val > 1) ret.push_back({val, 1});
  return ret;
}
void dfs(int i = 1, int p = 0) {
  int curRoot = root[p];
  for (auto x : fact[i])
    curRoot = update(curRoot, primeLeft[x.first] + x.second - 1, x.second);
  root[i] = curRoot;
  Lvl[i] = Lvl[p] + 1;
  P[i][0] = p;
  for (int j = 1; j < 18; j++) P[i][j] = P[P[i][j - 1]][j - 1];
  for (auto x : adj[i])
    if (x != p) dfs(x, i);
}
int LCA(int u, int v) {
  if (Lvl[u] < Lvl[v]) swap(u, v);
  int dist = Lvl[u] - Lvl[v];
  for (int i = 0; i < 18; i++)
    if (dist >> i & 1) u = P[u][i];
  for (int i = 17; i >= 0; i--)
    if (P[u][i] != P[v][i]) u = P[u][i], v = P[v][i];
  return u == v ? u : P[u][0];
}
long long fp(long long a, long long p) {
  long long r = 1;
  while (p) {
    if (p & 1) r = (r * a) % mod;
    a = (a * a) % mod;
    p >>= 1;
  }
  return r;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(primeLeft, -1, sizeof(primeLeft));
  for (int i = 2; i <= M; ++i) {
    if (lp[i] == 0) {
      lp[i] = i;
      PR.push_back(i);
    }
    for (int j = 0; j < (int)PR.size() && PR[j] <= lp[i] && i * PR[j] <= M; ++j)
      lp[i * PR[j]] = PR[j];
  }
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i], fact[i] = getPrimeFact(a[i]);
    for (auto x : fact[i]) st.insert(x.first);
  }
  MX = 25 * st.size();
  int c = 0;
  for (auto x : st) primeLeft[x] = c, c += 25;
  root[0] = copy();
  dfs();
  vector<pair<int, int> > f;
  int q;
  cin >> q;
  while (q--) {
    int u, v, x;
    cin >> u >> v >> x;
    f = getPrimeFact(x);
    int lca = LCA(u, v);
    int plca = P[lca][0];
    long long ans = 1;
    for (auto t : f) {
      if (primeLeft[t.first] == -1) continue;
      int index = primeLeft[t.first] + t.second - 1;
      pair<int, int> temp = {0, 0};
      temp.first += query(root[u], primeLeft[t.first], index).first;
      temp.first += query(root[v], primeLeft[t.first], index).first;
      temp.first -= query(root[lca], primeLeft[t.first], index).first;
      temp.first -= query(root[plca], primeLeft[t.first], index).first;
      temp.second += query(root[u], index + 1, primeLeft[t.first] + 24).second;
      temp.second += query(root[v], index + 1, primeLeft[t.first] + 24).second;
      temp.second -=
          query(root[lca], index + 1, primeLeft[t.first] + 24).second;
      temp.second -=
          query(root[plca], index + 1, primeLeft[t.first] + 24).second;
      ans = (ans * fp(t.first, temp.first + temp.second * t.second)) % mod;
    }
    cout << ans << endl;
  }
  return 0;
}