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 md = 1e9 + 7;
const int lim = 1e7 + 1;
long long mexp(long long a, long long e) {
  long long res = 1;
  while (e) {
    if (e & 1) res = res * a % md;
    a = a * a % md;
    e /= 2;
  }
  return res;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  vector<int> gpf(lim);
  for (int p = (2); p < (lim); ++p)
    if (!gpf[p])
      for (int i = p; i < lim; i += p) gpf[i] = p;
  int n;
  cin >> n;
  vector<vector<int>> gr(n);
  for (int _ = (1); _ < (n); ++_) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    gr[u].push_back(v);
    gr[v].push_back(u);
  }
  vector<int> a(n);
  for (auto &x : a) cin >> x;
  int lg = 0;
  while ((1 << lg) < n) ++lg;
  vector<vector<int>> hopp(lg, vector<int>(n, -1));
  vector<int> h(n);
  vector<int> lnum(n), rnum(n);
  int ix = 0;
  function<void(int)> dfs = [&](int v) {
    lnum[v] = ix++;
    for (auto &u : gr[v])
      if (u != hopp[0][v]) {
        hopp[0][u] = v;
        h[u] = h[v] + 1;
        dfs(u);
      }
    rnum[v] = ix++;
  };
  dfs(0);
  hopp[0][0] = 0;
  for (int i = (1); i < (lg); ++i)
    for (int j = (0); j < (n); ++j) hopp[i][j] = hopp[i - 1][hopp[i - 1][j]];
  auto jump = [&](int v, int l) {
    for (int i = 0; l; ++i) {
      if (l & (1 << i)) {
        v = hopp[i][v];
        l -= 1 << i;
      }
    }
    return v;
  };
  auto lca = [&](int u, int v) {
    if (h[u] > h[v]) swap(u, v);
    v = jump(v, h[v] - h[u]);
    if (u == v) return u;
    for (int i = (lg); i-- > (0);) {
      int vv = hopp[i][v], uu = hopp[i][u];
      if (vv != uu) {
        v = vv;
        u = uu;
      }
    }
    return hopp[0][u];
  };
  auto factor = [&](int m, vector<int> &res) {
    while (m > 1) {
      int p = gpf[m], q = 1;
      while (m % p == 0) {
        m /= p;
        q *= p;
        res.push_back(q);
      }
    }
  };
  vector<vector<pair<int, int>>> pre(lim);
  for (int i = (0); i < (n); ++i) {
    vector<int> ls;
    factor(a[i], ls);
    for (auto &q : ls) {
      pre[q].emplace_back(lnum[i], 1);
      pre[q].emplace_back(rnum[i], -1);
    }
  }
  for (auto &m : pre) {
    if (m.empty()) continue;
    sort((m).begin(), (m).end());
    for (int i = (1); i < (int(m.size())); ++i) m[i].second += m[i - 1].second;
  }
  auto cnt = [&](int q, int pos) {
    int lo = -1, hi = int(pre[q].size());
    while (lo + 1 < hi) {
      int mi = (lo + hi) / 2;
      if (pre[q][mi].first > pos)
        hi = mi;
      else
        lo = mi;
    }
    if (lo == -1) return 0;
    return pre[q][lo].second;
  };
  int q;
  cin >> q;
  for (int _ = (0); _ < (q); ++_) {
    int u, v, x;
    cin >> u >> v >> x;
    --u, --v;
    int w = lca(u, v);
    vector<int> ls;
    factor(x, ls);
    long long ans = 1;
    for (auto &q : ls) {
      if (pre[q].empty()) continue;
      int c = cnt(q, lnum[u]) + cnt(q, lnum[v]) - cnt(q, lnum[w]) -
              cnt(q, lnum[w] - 1);
      ans *= mexp(gpf[q], c);
      ans %= md;
    }
    cout << ans << endl;
  }
}