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 mod = (long long)1e9 + 7;
const int MX = (int)1e7 + 1;
const int N = (int)1e5 + 1;
long long qpow(long long a, int exp) {
  long long ans = 1;
  while (exp) {
    if (exp & 1) ans = ans * a % mod;
    a = a * a % mod;
    exp >>= 1;
  }
  return ans;
}
vector<int> pr;
vector<pair<int, int> > V[N << 3];
vector<pair<int, int> > Q[N << 3];
int d[MX];
void sieve() {
  memset(d, -1, sizeof d);
  for (int i = 2; i < MX; ++i) {
    if (d[i] >= 0) continue;
    for (int j = i; j < MX; j += i)
      if (d[j] < 0) d[j] = pr.size();
    pr.push_back(i);
  }
}
vector<int> g[N];
int p[N][20], h[N];
int st[N], en[N], cnt = 0;
void dfs(int u) {
  for (int i = 1; i < 17; ++i) p[u][i] = p[p[u][i - 1]][i - 1];
  st[u] = ++cnt;
  for (int v : g[u]) {
    if (v == p[u][0]) continue;
    p[v][0] = u;
    h[v] = h[u] + 1;
    dfs(v);
  }
  en[u] = cnt;
}
int par(int u, int d) {
  for (int i = 0; i < 17; ++i)
    if ((d >> i & 1)) u = p[u][i];
  return u;
}
int lca(int u, int v) {
  if (h[u] < h[v]) swap(u, v);
  u = par(u, h[u] - h[v]);
  if (u == v) return u;
  for (int i = 16; i >= 0; --i)
    if (p[u][i] != p[v][i]) {
      u = p[u][i];
      v = p[v][i];
    }
  return p[u][0];
}
int bit[N], n, m;
void add(int l, int r, int val) {
  for (; l <= n; l += l & -l) bit[l] += val;
  for (; r <= n; r += r & -r) bit[r] -= val;
}
int get(int P) {
  int ret = 0;
  for (; P > 0; P -= P & -P) ret += bit[P];
  return ret;
}
struct query {
  int u, v, z;
  long long ans;
} q[N];
void input() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    while (x > 1) {
      int P = d[x], exp = 0;
      while (d[x] == P) {
        exp++;
        x /= pr[P];
      }
      V[P].push_back(pair<int, int>(exp, i));
    }
  }
  dfs(1);
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    int u, v, x;
    scanf("%d%d%d", &u, &v, &x);
    while (x > 1) {
      int P = d[x], exp = 0;
      while (d[x] == P) {
        exp++;
        x /= pr[P];
      }
      Q[P].push_back(pair<int, int>(exp, i));
    }
    q[i].u = u;
    q[i].v = v;
    q[i].z = lca(u, v);
    q[i].ans = 1;
  }
  for (int i = 0; i < pr.size(); ++i) {
    sort(V[i].begin(), V[i].end());
    sort(Q[i].begin(), Q[i].end());
    reverse(V[i].begin(), V[i].end());
  }
}
void solve() {
  for (int i = 0; i < pr.size(); ++i) {
    int cur = 0, exp = 0;
    while (cur < Q[i].size()) {
      exp++;
      for (int j = 0; j < V[i].size(); ++j) {
        if (V[i][j].first < exp) break;
        int u = V[i][j].second;
        add(st[u], en[u] + 1, 1);
      }
      while (cur < Q[i].size() && Q[i][cur].first == exp) {
        int s = Q[i][cur++].second;
        int res = 0;
        res += get(st[q[s].u]);
        res += get(st[q[s].v]);
        res -= get(st[q[s].z]);
        res -= get(st[p[q[s].z][0]]);
        q[s].ans = q[s].ans * qpow(pr[i], res) % mod;
      }
    }
    while (exp > 0) {
      for (int j = 0; j < V[i].size(); ++j) {
        if (V[i][j].first < exp) break;
        int u = V[i][j].second;
        add(st[u], en[u] + 1, -1);
      }
      exp--;
    }
  }
  for (int i = 1; i <= m; ++i) printf("%lld\n", q[i].ans);
}
int main() {
  sieve();
  input();
  solve();
}