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 mo = 1000000007;
const int M = 10000005;
const int H = 670000;
const int N = 100005;
const int W = 10;
int n, q, i, j, k, x, y, z, p, tot, ma;
int a[N * 2], b[N][3];
int lst[H], e[N * 16][3], p1[N], p2[N];
int fa[N][W + 1], dep[N];
int d[N * 2][2], f[N], f2[N];
long long ans[N], sum[N], t;
int h[H], g[M];
bool bz[M];
void link(int x, int y, int z) {
  tot++, e[tot][0] = lst[x], e[tot][1] = y, e[tot][2] = z, lst[x] = tot;
}
void qsort(int l, int r) {
  int i = l, j = r, t = (l + r) >> 1, mid = d[t][1], mid2 = d[t][0];
  while (i <= j) {
    while (d[i][1] < mid || (d[i][1] == mid && d[i][0] < mid2)) i++;
    while (d[j][1] > mid || (d[j][1] == mid && d[j][0] > mid2)) j--;
    if (i <= j) {
      swap(d[i][1], d[j][1]), swap(d[i][0], d[j][0]), i++, j--;
    }
  }
  if (l < j) qsort(l, j);
  if (i < r) qsort(i, r);
}
void build(int x) {
  p1[x] = ++tot;
  int i = lst[x];
  while (i) {
    int y = e[i][1];
    i = e[i][0];
    if (fa[x][0] != y) {
      fa[y][0] = x, dep[y] = dep[x] + 1, build(y);
    }
  }
  p2[x] = tot;
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = W; i >= 0; i--)
    while (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
  if (x == y) return x;
  for (int i = W; i >= 0; i--)
    while (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}
void ins(int x, int i, int j) {
  while (x <= n) f[x] += i, f2[x] += j, x += x & (-x);
}
int find(int x, int i, int j) {
  int q = 0;
  while (x) q += f[x] * i + f2[x] * j, x -= x & (-x);
  return q;
}
long long ksm(long long x, long long y) {
  long long q = 1;
  while (y) {
    if (y & 1) q = q * x % mo;
    y = y >> 1, x = x * x % mo;
  }
  return q;
}
int main() {
  scanf("%d", &n);
  for (i = 2; i <= n; i++) scanf("%d%d", &x, &y), link(x, y, 0), link(y, x, 0);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), ma = max(ma, a[i]);
  dep[1] = 1, tot = 0, build(1);
  for (j = 1; j <= W; j++)
    for (i = 1; i <= n; i++) fa[i][j] = fa[fa[i][j - 1]][j - 1];
  scanf("%d", &q);
  for (i = 1; i <= q; i++) {
    scanf("%d%d%d", &x, &y, &a[i + n]), ans[i] = 1, ma = max(ma, a[i + n]);
    b[i][1] = x, b[i][2] = y, b[i][3] = lca(x, y);
  }
  for (i = 2; i <= ma; i++) {
    if (!bz[i]) h[0]++, h[h[0]] = i, g[i] = h[0];
    j = 1;
    while (j <= h[0] && i * h[j] <= ma) {
      t = i * h[j];
      if (!bz[t]) g[t] = j, bz[t] = 1;
      if (i % h[j] == 0) break;
      j++;
    }
  }
  memset(lst, 0, sizeof(lst));
  tot = 0;
  for (i = 1; i <= n + q; i++) {
    while (a[i] > 1) {
      k = 0, t = g[a[i]];
      while (a[i] % h[t] == 0) a[i] /= h[t], k++;
      link(t, i, k);
    }
  }
  for (i = 1; i <= h[0]; i++)
    if (lst[i]) {
      j = lst[i], tot = 0;
      while (j) tot++, d[tot][1] = e[j][2], d[tot][0] = e[j][1], j = e[j][0];
      qsort(1, tot);
      for (j = 1; j <= tot; j++) {
        k = d[j][0];
        if (k > n) {
          k -= n, p = d[j][1], x = b[k][1], y = b[k][2], z = b[k][3];
          sum[k] = find(p1[x], -p, 1) + find(p1[y], -p, 1) -
                   find(p1[z], -p, 1) - find(p1[fa[z][0]], -p, 1);
        } else
          p = d[j][1], ins(p1[k], 1, p), ins(p2[k] + 1, -1, -p);
      }
      for (j = 1; j <= tot; j++) {
        k = d[j][0];
        if (k > n) {
          k -= n, x = b[k][1], y = b[k][2], z = b[k][3];
          p = find(p1[x], 1, 0) + find(p1[y], 1, 0) - find(p1[z], 1, 0) -
              find(p1[fa[z][0]], 1, 0);
          ans[k] = ans[k] * ksm(h[i], p * d[j][1] + sum[k]) % mo;
        }
      }
      for (j = 1; j <= tot; j++) {
        k = d[j][0];
        if (k <= n) p = d[j][1], ins(p1[k], -1, -p), ins(p2[k] + 1, 1, p);
      }
    }
  for (i = 1; i <= q; i++) printf("%I64d\n", ans[i]);
  return 0;
  fclose(stdin);
  fclose(stdout);
}