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 mod = 1000000007;
int qpow(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1) res = 1ll * res * x % mod;
    x = 1ll * x * x % mod;
    y >>= 1;
  }
  return res;
}
int inv(int x) { return qpow(x, mod - 2); }
const int N = 100000, QU = 100000, V = 10000000, LOG_N = 17, LOG_V = 24;
int n, qu;
vector<int> nei[N + 1];
vector<pair<int, int> > a[N + 1];
int ans[QU + 1];
int fa[N + 1][LOG_N], dep[N + 1];
void dfs(int x = 1) {
  for (int i = 1; i < LOG_N; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
  for (int i = 0; i < nei[x].size(); i++) {
    int y = nei[x][i];
    if (y == fa[x][0]) continue;
    fa[y][0] = x;
    dep[y] = dep[x] + 1;
    dfs(y);
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = LOG_N - 1; ~i; i--)
    if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
  if (x == y) return x;
  for (int i = LOG_N - 1; ~i; i--)
    if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}
int now, pid[V + 1];
void sieve() {
  for (int i = 2; i <= V; i++) {
    if (pid[i] == -1) continue;
    pid[i] = ++now;
    for (int j = i + i; j <= V; j += i) pid[j] = -1;
  }
}
vector<pair<int, int> > ctb[N + 1];
int cnt[V / 5 + 1][LOG_V], tot[V / 5 + 1];
void dfs0(int x = 1) {
  for (int i = 0; i < a[x].size(); i++)
    cnt[pid[a[x][i].first]][a[x][i].second]++, tot[pid[a[x][i].first]]++;
  for (int i = 0; i < ctb[x].size(); i++) {
    int qid = ctb[x][i].first, sgn = ctb[x][i].second < 0 ? -1 : 1,
        v = abs(ctb[x][i].second);
    for (int j = 2; j * j <= v; j++)
      if (v % j == 0) {
        int hav = 0, sum = 0, all = 0;
        while (v % j == 0) hav++, v /= j;
        for (int k = 1; k <= hav; k++)
          all += k * cnt[pid[j]][k], sum += cnt[pid[j]][k];
        all += hav * (tot[pid[j]] - sum);
        ans[qid] = 1ll * ans[qid] *
                   (sgn == 1 ? qpow(j, all) : inv(qpow(j, all))) % mod;
      }
    if (v > 1) {
      int hav = 1, sum = 0, all = 0;
      for (int k = 1; k <= hav; k++)
        all += k * cnt[pid[v]][k], sum += cnt[pid[v]][k];
      all += hav * (tot[pid[v]] - sum);
      ans[qid] =
          1ll * ans[qid] * (sgn == 1 ? qpow(v, all) : inv(qpow(v, all))) % mod;
    }
  }
  for (int i = 0; i < nei[x].size(); i++) {
    int y = nei[x][i];
    if (y == fa[x][0]) continue;
    dfs0(y);
  }
  for (int i = 0; i < a[x].size(); i++)
    cnt[pid[a[x][i].first]][a[x][i].second]--, tot[pid[a[x][i].first]]--;
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    nei[x].push_back(y);
    nei[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    for (int j = 2; j * j <= x; j++)
      if (x % j == 0) {
        a[i].push_back(make_pair(j, 0));
        while (x % j == 0) a[i].back().second++, x /= j;
      }
    if (x > 1) a[i].push_back(make_pair(x, 1));
  }
  dep[1] = 1, dfs();
  sieve();
  cin >> qu;
  for (int i = 1; i <= qu; i++) {
    ans[i] = 1;
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    int _lca = lca(x, y);
    ctb[fa[_lca][0]].push_back(make_pair(i, -v)),
        ctb[_lca].push_back(make_pair(i, -v));
    ctb[x].push_back(make_pair(i, v)), ctb[y].push_back(make_pair(i, v));
  }
  dfs0();
  for (int i = 1; i <= qu; i++) printf("%d\n", ans[i]);
  return 0;
}