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 N = 100005, Q = 100005, MX = 1E7 + 5, PR = 670000, MOD = 1E9 + 7;
vector<int> pro, sav, adj[N];
vector<pair<int, int>> val[PR];
int n, q, u, v, x, pr = 0, cnt = 0, cur = 1, a[N], h[N], rt[N], par[N], col[N],
                   sub[N], in[N], d[MX], pos[MX];
long long ans[Q];
class CBIT {
 private:
  int bit[N];

 public:
  void build() { fill(bit, bit + N, 0); }
  void upd(int u, int v) {
    for (; u <= n; u += u & -u) bit[u] += v;
  }
  int que(int u) {
    int ret = 0;
    for (; u > 0; u -= u & -u) ret += bit[u];
    return ret;
  }
  int que(int l, int r) { return que(r) - que(l - 1); }
} sum, chk;
struct SQuery {
  int u, v, x, ind;
};
vector<SQuery> que[PR];
long long pw(int u, int p) {
  long long ret = 1;
  for (int i = 31 - __builtin_clz(p); i >= 0; i--) {
    (ret *= ret) %= MOD;
    if (p >> i & 1) (ret *= u) %= MOD;
  }
  return ret;
}
void init() {
  sum.build();
  chk.build();
  h[0] = -1;
  for (int i = 2; i < MX; i++)
    if (d[i] == 0) {
      sav.push_back(i);
      pos[i] = ++pr;
      d[i] = i;
      for (int j = i; j < MX / i; j++) d[i * j] = i;
    }
}
int DFS(int u, int p = 0) {
  h[u] = h[p] + 1;
  par[u] = p;
  sub[u] = 1;
  for (int &v : adj[u])
    if (v != p) sub[u] += DFS(v, u);
  return sub[u];
}
void HLD(int u, int p = 0) {
  in[u] = ++cnt;
  col[u] = cur;
  if (rt[cur] == 0) rt[cur] = u;
  int mc = 0;
  for (int &v : adj[u])
    if (v != p && sub[v] > sub[mc]) mc = v;
  if (mc != 0) HLD(mc, u);
  for (int &v : adj[u])
    if (v != p && v != mc) {
      ++cur;
      HLD(v, u);
    }
}
void get_ans(const SQuery &a, int pr) {
  int u = a.u, v = a.v, su = 0, ch = 0;
  while (col[u] != col[v]) {
    if (h[rt[col[u]]] < h[rt[col[v]]]) swap(u, v);
    su += sum.que(in[rt[col[u]]], in[u]);
    ch += chk.que(in[rt[col[u]]], in[u]);
    u = par[rt[col[u]]];
  }
  if (h[u] < h[v]) swap(u, v);
  su += sum.que(in[v], in[u]);
  ch += chk.que(in[v], in[u]);
  (ans[a.ind] *= pw(pr, su + ch * a.x)) %= MOD;
}
int main() {
  init();
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  DFS(1);
  HLD(1);
  for (int i = 1; i <= n; i++) {
    for (scanf("%d", &x); x != 1; x /= d[x]) pro.push_back(d[x]);
    sort(pro.begin(), pro.end());
    pro.push_back(-1);
    int cnt = 1;
    for (unsigned int j = 1; j < pro.size(); j++) {
      if (pro[j] == pro[j - 1])
        cnt++;
      else {
        val[pos[pro[j - 1]]].push_back({cnt, i});
        cnt = 1;
      }
    }
    pro.clear();
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    ans[i] = 1;
    for (scanf("%d%d%d", &u, &v, &x); x != 1; x /= d[x]) pro.push_back(d[x]);
    sort(pro.begin(), pro.end());
    pro.push_back(-1);
    int cnt = 1;
    for (unsigned int j = 1; j < pro.size(); j++) {
      if (pro[j] == pro[j - 1])
        cnt++;
      else {
        que[pos[pro[j - 1]]].push_back((SQuery){u, v, cnt, i});
        cnt = 1;
      }
    }
    pro.clear();
  }
  for (int i = 1; i <= pr; i++) {
    if (que[i].empty() || val[i].empty()) continue;
    sort(que[i].begin(), que[i].end(),
         [](const SQuery &a, const SQuery &b) { return a.x > b.x; });
    sort(val[i].begin(), val[i].end(), greater<pair<int, int>>());
    for (pair<int, int> &u : val[i]) sum.upd(in[u.second], u.first);
    unsigned int pt = 0;
    for (SQuery &u : que[i]) {
      for (; pt < val[i].size() && val[i][pt].first >= u.x; pt++) {
        sum.upd(in[val[i][pt].second], -val[i][pt].first);
        chk.upd(in[val[i][pt].second], 1);
      }
      get_ans(u, sav[i - 1]);
    }
    for (unsigned int j = 0; j < pt; j++) chk.upd(in[val[i][j].second], -1);
    for (unsigned int j = pt; j < val[i].size(); j++)
      sum.upd(in[val[i][j].second], -val[i][j].first);
  }
  for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
}