986_E. Prince's 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

Example input:
4
1 2
1 3
1 4
6 4 9 5
3
2 3 6
2 3 2
3 4 7

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


Example output:
36
4
1

196000
12250
999998215


Tags: brute force
data structures
math
number theory
trees
Difficulty rating: 2800

Solution:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 14, maxv = 1e7 + 1, lg = 17, lgv = 25, mod = 1e9 + 7;
int n, q, a[maxn], fp[maxv], par[lg][maxn], h[maxn];
bool pr[maxv];
vector<int> g[maxn];
int *num[maxv];
struct AQ {
  int id, cuf;
};
struct PrimeData {
  int p, have, sum;
};
vector<PrimeData> prs[maxn];
vector<AQ> assign[maxn];
void dfs(int v = 0) {
  for (auto u : g[v])
    if (u != par[0][v]) {
      h[u] = h[v] + 1;
      par[0][u] = v;
      dfs(u);
    }
}
int lca(int v, int u) {
  if (h[u] < h[v]) swap(v, u);
  for (int i = 0; i < lg; i++)
    if (h[u] - h[v] >> i & 1) u = par[i][u];
  for (int i = lg - 1; i >= 0; i--)
    if (par[i][v] != par[i][u]) {
      v = par[i][v];
      u = par[i][u];
    }
  return v == u ? v : par[0][v];
}
int po(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = (long long)a * a % mod)
    if (b & 1) ans = (long long)ans * a % mod;
  return ans;
}
void add(int x, int v) {
  while (x != 1) {
    int p = fp[x], c = 0;
    while (x % p == 0) {
      c++;
      x /= p;
    }
    num[p][c] += v;
  }
}
void solve(int v = 0) {
  add(a[v], 1);
  for (auto [i, c] : assign[v])
    for (auto &[p, h, s] : prs[i])
      for (int i = 1; i < lgv; i++) s += c * min(h, i) * num[p][i];
  for (auto u : g[v])
    if (u != par[0][v]) solve(u);
  add(a[v], -1);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  for (int i = 2; i < maxv; i++)
    if (!pr[i]) {
      num[i] = new int[lgv];
      fill(num[i], num[i] + lgv, 0);
      fp[i] = i;
      for (long long j = (long long)i * i; j < maxv; j += i) {
        pr[j] = 1;
        fp[j] = i;
      }
    }
  cin >> n;
  for (int i = 1; i < n; i++) {
    int v, u;
    cin >> v >> u;
    v--, u--;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  for (int i = 0; i < n; i++) cin >> a[i];
  dfs();
  for (int i = 1; i < lg; i++)
    for (int v = 0; v < n; v++) par[i][v] = par[i - 1][par[i - 1][v]];
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, u, x;
    cin >> v >> u >> x;
    v--, u--;
    int p = lca(v, u);
    assign[v].push_back({i, 1});
    assign[u].push_back({i, 1});
    assign[p].push_back({i, -1});
    if (p) assign[par[0][p]].push_back({i, -1});
    while (x != 1) {
      int p = fp[x], c = 0;
      while (x % p == 0) {
        c++;
        x /= p;
      }
      prs[i].push_back({p, c, 0});
    }
  }
  solve();
  for (int i = 0; i < q; i++) {
    int ans = 1;
    for (auto [p, h, s] : prs[i]) ans = (long long)ans * po(p, s) % mod;
    cout << ans << '\n';
  }
}