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;
#pragma optimize("-O3")
int n, l;
const int N = 1e5 + 5;
const int M = 1e7 + 5;
const long long int MOD = 1e9 + 7;
long long int ans[N];
vector<pair<int, int>> qu;
vector<vector<int>> adj;
vector<vector<pair<pair<int, int>, int>>> pr;
int a[N];
int sv[M];
int t[4 * N + 5];
int timer;
vector<int> tin, tout;
vector<vector<int>> up;
int ind[N];
map<pair<int, int>, int> ma;
void dfs(int v, int p) {
  timer++;
  tin[v] = timer;
  ind[v] = timer;
  up[v][0] = p;
  for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
  for (int u : adj[v]) {
    if (u != p) dfs(u, v);
  }
  tout[v] = timer;
}
bool is_ancestor(int u, int v) {
  return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v) {
  if (is_ancestor(u, v)) return u;
  if (is_ancestor(v, u)) return v;
  for (int i = l; i >= 0; --i) {
    if (!is_ancestor(up[u][i], v)) u = up[u][i];
  }
  return up[u][0];
}
void preprocess(int root) {
  tin.resize(n + 1);
  tout.resize(n + 1);
  timer = 0;
  l = ceil(log2(n + 1));
  up.assign(n + 1, vector<int>(l + 1));
  dfs(root, root);
}
int sum(int v, int tl, int tr, int l, int r) {
  if (l > r) return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  return sum(v * 2, tl, tm, l, min(r, tm)) +
         sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}
void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[v] += new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
}
long long int add(int x, int y) { return (x + y) % MOD; }
long long int sub(int x, int y) { return add(x, MOD - y); }
long long int mul(int x, int y) { return (x * 1ll * y) % MOD; }
long long int binpow(int x, int y) {
  long long int z = 1;
  while (y > 0) {
    if (y % 2 == 1) z = mul(z, x);
    x = mul(x, x);
    y /= 2;
  }
  return z;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  adj.resize(n + 1);
  pr.resize(M);
  for (int i = 2; i <= 10000000; i++) {
    if (sv[i] != 0) {
      continue;
    }
    for (int j = 1; j * i <= 10000000; j++) {
      if (sv[i * j] == 0) {
        sv[i * j] = i;
      }
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  preprocess(1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    int val = a[i];
    vector<pair<int, int>> vec;
    while (val != 1) {
      int v2 = sv[val];
      val = val / v2;
      if (vec.empty()) {
        vec.push_back({v2, 1});
      } else if (vec.back().first == v2) {
        int num = vec.back().second;
        vec.pop_back();
        vec.push_back({v2, num + 1});
      } else {
        vec.push_back({v2, 1});
      }
    }
    for (auto u : vec) {
      pr[u.first].push_back({{u.second, 0}, i});
      ma[{u.first, i}] = u.second;
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ans[i] = 1;
  }
  for (int i = 0; i < q; i++) {
    int u, v, x;
    cin >> u >> v >> x;
    qu.push_back({u, v});
    int val = x;
    vector<pair<int, int>> vec;
    while (val != 1) {
      int v2 = sv[val];
      val = val / v2;
      if (vec.empty()) {
        vec.push_back({v2, 1});
      } else if (vec.back().first == v2) {
        int num = vec.back().second;
        vec.pop_back();
        vec.push_back({v2, num + 1});
      } else {
        vec.push_back({v2, 1});
      }
    }
    for (auto u : vec) {
      pr[u.first].push_back({{u.second, 1}, i});
    }
  }
  for (int i = 0; i < M; i++) {
    sort(pr[i].begin(), pr[i].end());
  }
  for (int i = 1; i <= M; i++) {
    if (pr[i].size() == 0) {
      continue;
    }
    vector<int> upd;
    int curr = 0;
    for (int j = 0; j < pr[i].size(); j++) {
      int inc;
      if (pr[i][j].first.first > curr) {
        for (int k = j; k < pr[i].size(); k++) {
          inc = pr[i][j].first.first - curr;
          if (pr[i][k].first.second == 0) {
            update(1, 0, N, tin[pr[i][k].second], inc);
            update(1, 0, N, tout[pr[i][k].second] + 1, -inc);
            upd.push_back(tin[pr[i][k].second]);
            upd.push_back(tout[pr[i][k].second] + 1);
          }
        }
        curr = pr[i][j].first.first;
      }
      if (pr[i][j].first.second == 1) {
        int quer = pr[i][j].second;
        int u = qu[quer].first;
        int v = qu[quer].second;
        int lc = lca(u, v);
        int val = sum(1, 0, N, 0, tin[u]) + sum(1, 0, N, 0, tin[v]) -
                  2 * sum(1, 0, N, 0, tin[lc]) +
                  min(ma[{i, lc}], pr[i][j].first.first);
        ans[quer] = mul(ans[quer], binpow(i, val));
      }
    }
    for (auto u : upd) {
      int val2 = sum(1, 0, N, u, u);
      update(1, 0, N, u, -val2);
    }
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}