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;
long long read() {
  char ch = getchar();
  long long x = 0;
  int op = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') op = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
  return x * op;
}
int n, Q, cnt, head[100005], tot, p[10000005 / 10], mif[10000005], clk,
    in[100005], out[100005], dep[100005], f[100005][25], ans[100005];
bool vis[10000005];
struct edge {
  int to, nxt;
} e[100005 << 1];
struct quest {
  int x, y, z;
} q[100005];
vector<int> num[2][10000005 / 10], pw[2][10000005 / 10], t[2][30];
void adde(int x, int y) {
  e[++cnt].to = y;
  e[cnt].nxt = head[x];
  head[x] = cnt;
}
void ins(int x, int y) {
  adde(x, y);
  adde(y, x);
}
int ksm(int x, long long p) {
  int ret = 1;
  for (; p; p >>= 1, x = (long long)x * x % 1000000007)
    if (p & 1) ret = (long long)ret * x % 1000000007;
  return ret;
}
void init(int n) {
  for (int i = (2); i <= (n); i++) {
    if (!mif[i]) p[++tot] = i, mif[i] = tot;
    for (int j = 1; j <= tot && (long long)i * p[j] <= n; j++) {
      mif[i * p[j]] = j;
      if (i % p[j] == 0) break;
    }
  }
}
void dfs(int u, int pr) {
  in[u] = ++clk;
  for (int i = (1); i <= (20); i++) f[u][i] = f[f[u][i - 1]][i - 1];
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == pr) continue;
    f[v][0] = u;
    dep[v] = dep[u] + 1;
    dfs(v, u);
  }
  out[u] = clk;
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int tmp = dep[x] - dep[y];
  for (int i = (20); i >= (0); i--)
    if (tmp >> i & 1) x = f[x][i];
  if (x == y) return x;
  for (int i = (20); i >= (0); i--)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
struct bit {
  int t[100005];
  void add(int x, int val) {
    for (int i = x; i <= n; i += i & -i) t[i] += val;
  }
  int qry(int x) {
    int ret = 0;
    for (int i = x; i; i -= i & -i) ret += t[i];
    return ret;
  }
} tr[2];
int main() {
  n = read();
  init(10000000);
  for (int i = (1); i <= (n - 1); i++) {
    int x = read(), y = read();
    ins(x, y);
  }
  dfs(1, 0);
  for (int i = (1); i <= (n); i++) {
    int x = read();
    while (x != 1) {
      int tmp = mif[x], jsq = 0;
      while (x % p[tmp] == 0) x /= p[tmp], jsq++;
      num[0][tmp].push_back(i);
      pw[0][tmp].push_back(jsq);
    }
  }
  Q = read();
  for (int i = (1); i <= (Q); i++) {
    q[i].x = read(), q[i].y = read(), q[i].z = read();
    int x = q[i].z;
    while (x != 1) {
      int tmp = mif[x], jsq = 0;
      while (x % p[tmp] == 0) x /= p[tmp], jsq++;
      num[1][tmp].push_back(i);
      pw[1][tmp].push_back(jsq);
    }
    ans[i] = 1;
  }
  for (int i = (1); i <= (tot); i++) {
    int Mx = 0;
    for (auto j : pw[0][i]) Mx = max(Mx, j);
    for (auto j : pw[1][i]) Mx = max(Mx, j);
    if (!Mx) continue;
    for (int j = (1); j <= (Mx); j++) t[0][j].clear(), t[1][j].clear();
    for (int j = (0); j <= (((int)(num[0][i]).size()) - 1); j++) {
      t[0][pw[0][i][j]].push_back(num[0][i][j]);
      tr[1].add(in[num[0][i][j]], 1);
      tr[1].add(out[num[0][i][j]] + 1, -1);
    }
    for (int j = (0); j <= (((int)(num[1][i]).size()) - 1); j++)
      t[1][pw[1][i][j]].push_back(num[1][i][j]);
    for (int j = (1); j <= (Mx); j++) {
      for (auto k : t[1][j]) {
        int x = q[k].x, y = q[k].y, z = lca(x, y), w = f[z][0];
        long long now = tr[0].qry(in[x]) + (long long)tr[1].qry(in[x]) * j;
        now += tr[0].qry(in[y]) + (long long)tr[1].qry(in[y]) * j;
        now -= tr[0].qry(in[z]) + (long long)tr[1].qry(in[z]) * j;
        if (w) now -= tr[0].qry(in[w]) + (long long)tr[1].qry(in[w]) * j;
        now = (now % 1000000007 + 1000000007) % 1000000007;
        ans[k] = (long long)ans[k] * ksm(p[i], now) % 1000000007;
      }
      for (auto k : t[0][j]) {
        tr[0].add(in[k], j);
        tr[0].add(out[k] + 1, -j);
        tr[1].add(in[k], -1);
        tr[1].add(out[k] + 1, 1);
      }
    }
    for (int j = (0); j <= (((int)(num[0][i]).size()) - 1); j++) {
      tr[0].add(in[num[0][i][j]], -pw[0][i][j]);
      tr[0].add(out[num[0][i][j]] + 1, pw[0][i][j]);
    }
  }
  for (int i = (1); i <= (Q); i++) printf("%d\n", ans[i]);
  return 0;
}