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 maxn = 1e5 + 1000;
const int lim = 1e7;
const int mod = 1e9 + 7;
int dep[maxn], prime[lim + 10], cnt, idx[lim + 10], n;
int buc[665577][30], val[maxn], f[maxn][21];
void init1() {
  for (int i = (2), iend = (lim); i <= iend; i++)
    if (!prime[i]) {
      prime[++cnt] = i, idx[i] = cnt;
      for (int j = 2 * i; j <= lim; j += i) prime[j] = 1;
    }
}
vector<int> side[maxn];
vector<pair<int, int> > q[maxn], off[maxn];
vector<pair<int, int> > ans[maxn];
void dfs1(int u, int fa) {
  f[u][0] = fa;
  dep[u] = dep[fa] + 1;
  for (int i = 0; i < side[u].size(); i++) {
    int v = side[u][i];
    if (v == fa) continue;
    dfs1(v, u);
  }
}
void dfs2(int u, int fa) {
  vector<pair<int, int> > tmp;
  tmp.clear();
  int res = val[u];
  for (int i = 2; i <= sqrt(val[u]); i++) {
    int cnt = 0;
    while (res % i == 0) {
      res /= i;
      cnt++;
      tmp.push_back(make_pair(idx[i], cnt)), buc[idx[i]][cnt]++;
    }
  }
  if (res > 1) tmp.push_back(make_pair(idx[res], 1)), buc[idx[res]][1]++;
  for (int i = 0; i < off[u].size(); i++) {
    int qid = off[u][i].first, ty = off[u][i].second;
    for (int j = 0; j < q[qid].size(); j++) {
      int p_idx = q[qid][j].first, cnt = q[qid][j].second;
      for (int k = (1), kend = (cnt); k <= kend; k++) {
        ans[qid][j].second += ty * buc[p_idx][k];
      }
    }
  }
  for (int i = 0; i < side[u].size(); i++) {
    int v = side[u][i];
    if (v == fa) continue;
    dfs2(v, u);
  }
  for (int i = 0; i < tmp.size(); i++) {
    buc[tmp[i].first][tmp[i].second]--;
  }
}
void init2() {
  for (int i = (1), iend = (20); i <= iend; i++)
    for (int j = (1), jend = (n); j <= jend; j++)
      f[j][i] = f[f[j][i - 1]][i - 1];
}
int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  for (int i = 20; i >= 0; i--)
    if (dep[f[u][i]] >= dep[v]) u = f[u][i];
  if (u == v) return u;
  for (int i = 20; i >= 0; i--)
    if (f[u][i] != f[v][i]) u = f[u][i], v = f[v][i];
  return f[u][0];
}
int ksm(int num, int t) {
  int res = 1;
  for (; t; t >>= 1, num = 1ll * num * num % mod) {
    if (t & 1) res = 1ll * num * res % mod;
  }
  return res;
}
long long ggg[maxn];
int gcd(int a, int b) { return (!b) ? a : gcd(b, a % b); }
int main() {
  init1();
  scanf("%d", &n);
  for (int i = (1), iend = (n - 1); i <= iend; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    side[u].push_back(v);
    side[v].push_back(u);
  }
  for (int i = (1), iend = (n); i <= iend; i++) scanf("%d", &val[i]);
  dfs1(1, 0);
  init2();
  int gg;
  scanf("%d", &gg);
  for (int i = (1), iend = (gg); i <= iend; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    int l = lca(u, v);
    int w;
    scanf("%d", &w);
    off[u].push_back(make_pair(i, 1));
    off[v].push_back(make_pair(i, 1));
    off[l].push_back(make_pair(i, -2));
    ggg[i] = gcd(val[l], w);
    int res = w;
    for (int j = 2; j <= sqrt(w); j++) {
      int cnt = 0;
      while (res % j == 0) res /= j, cnt++;
      if (cnt)
        q[i].push_back(make_pair(idx[j], cnt)),
            ans[i].push_back(make_pair(idx[j], 0));
    }
    if (res > 1)
      q[i].push_back(make_pair(idx[res], 1)),
          ans[i].push_back(make_pair(idx[res], 0));
  }
  dfs2(1, 0);
  for (int i = (1), iend = (gg); i <= iend; i++) {
    long long res = ggg[i];
    for (int j = 0; j < ans[i].size(); j++) {
      int p = prime[ans[i][j].first], cnt = ans[i][j].second;
      res = res * ksm(p, cnt) % mod;
    }
    printf("%lld\n", res);
  }
  return 0;
}