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 MAX_N = 1e5 + 5;
const int MOD = 1e9 + 7;
constexpr int MAX_VAL = 1e7 + 5;
constexpr int LOGMAX = 31 - __builtin_clz(MAX_N) + 5;
bool prime[MAX_VAL];
vector<int> primes, adj[MAX_N];
vector<tuple<int, int, int, int> > ops[MAX_VAL];
int st[MAX_N], en[MAX_N], arr[MAX_N], ans[MAX_N], p[LOGMAX][MAX_N],
    level[MAX_N];
int less_bit[MAX_N], val_bit[MAX_N], zero_bit[MAX_N];
int dfs_c = 1, n;
void update(int idx, int val, int bit[MAX_N]) {
  while (idx < MAX_N) {
    bit[idx] += val;
    idx += idx & -idx;
  }
}
int query(int idx, int bit[MAX_N]) {
  int total = 0;
  while (idx) {
    total += bit[idx];
    idx -= idx & -idx;
  }
  return total;
}
void dfs(int node, int father) {
  st[node] = dfs_c++;
  for (auto &each : adj[node]) {
    if (each != father) {
      p[0][each] = node;
      level[each] = level[node] + 1;
      dfs(each, node);
    }
  }
  en[node] = dfs_c - 1;
}
inline int add(int a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
  return a;
}
inline int mult(int a, int b) { return (1LL * a * b) % MOD; }
int f_exp(int x, int exp) {
  if (exp == 0) {
    return 1;
  } else if (exp & 1) {
    return mult(x, f_exp(x, exp - 1));
  }
  return f_exp(mult(x, x), exp / 2);
}
inline int inv(int x) { return f_exp(x, MOD - 2); }
void sieve() {
  memset(prime, true, sizeof prime);
  primes.push_back(2);
  for (long long i = 3; i < MAX_VAL; i += 2) {
    if (prime[i]) {
      primes.push_back(i);
      for (long long j = i * i; j < MAX_VAL; j += 2 * i) {
        prime[j] = false;
      }
    }
  }
}
int lca(int u, int v) {
  if (level[u] > level[v]) {
    swap(u, v);
  }
  int lg = 31 - __builtin_clz(level[v]);
  for (int i = int(lg); i >= int(0); i--) {
    if (level[v] - (1 << i) >= level[u]) {
      v = p[i][v];
    }
  }
  if (u == v) {
    return u;
  }
  lg = 31 - __builtin_clz(level[v]);
  for (int i = int(lg); i >= int(0); i--) {
    if (p[i][u] != p[i][v]) {
      u = p[i][u];
      v = p[i][v];
    }
  }
  return p[0][u];
}
int main() {
  sieve();
  scanf("%d", &n);
  for (int i = int(0); i < int(n - 1); i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  memset(p, -1, sizeof p);
  dfs(1, -1);
  for (int i = int(1); i < int(LOGMAX); i++) {
    for (int j = int(1); j < int(n + 1); j++) {
      if (p[i - 1][j] != -1) {
        p[i][j] = p[i - 1][p[i - 1][j]];
      }
    }
  }
  for (int i = int(1); i < int(n + 1); i++) {
    scanf("%d", arr + i);
    int x = arr[i];
    for (int j = 0; j < (int)primes.size() && primes[j] * primes[j] <= x; j++) {
      int e = 0;
      while (x % primes[j] == 0) {
        e++;
        x /= primes[j];
      }
      if (e) {
        ops[primes[j]].emplace_back(e, 0, i, -1);
      }
    }
    if (x != 1) {
      ops[x].emplace_back(1, 0, i, -1);
    }
  }
  int q;
  scanf("%d", &q);
  for (int i = int(0); i < int(q); i++) {
    int u, v, x;
    scanf("%d %d %d", &u, &v, &x);
    int l = lca(u, v);
    for (int j = 0; j < (int)primes.size() && primes[j] * primes[j] <= x; j++) {
      int e = 0;
      while (x % primes[j] == 0) {
        e++;
        x /= primes[j];
      }
      if (e) {
        ops[primes[j]].emplace_back(e, 1, u, i);
        ops[primes[j]].emplace_back(e, 1, v, i);
        ops[primes[j]].emplace_back(-e, 1, l, i);
        if (p[0][l] != -1) {
          ops[primes[j]].emplace_back(-e, 1, p[0][l], i);
        }
      }
    }
    if (x != 1) {
      ops[x].emplace_back(1, 1, u, i);
      ops[x].emplace_back(1, 1, v, i);
      ops[x].emplace_back(-1, 1, l, i);
      if (p[0][l] != -1) {
        ops[x].emplace_back(-1, 1, p[0][l], i);
      }
    }
    ans[i] = 1;
  }
  for (auto &each : primes) {
    if (ops[each].empty()) {
      continue;
    }
    sort(ops[each].begin(), ops[each].end(),
         [](const tuple<int, int, int, int> &lhs,
            const tuple<int, int, int, int> &rhs) {
           if (abs(get<0>(lhs)) != abs(get<0>(rhs))) {
             return abs(get<0>(lhs)) < abs(get<0>(rhs));
           }
           tuple<int, int, int, int> a = tuple<int, int, int, int>(
               0, get<1>(lhs), get<2>(lhs), get<3>(lhs));
           tuple<int, int, int, int> b = tuple<int, int, int, int>(
               0, get<1>(rhs), get<2>(rhs), get<3>(rhs));
           return a < b;
         });
    for (auto &op : ops[each]) {
      int exp, type, node, q_id;
      tie(exp, type, node, q_id) = op;
      if (type == 0) {
        int l = st[node];
        int r = en[node];
        update(l, 1, zero_bit);
        update(r + 1, -1, zero_bit);
      }
    }
    for (auto &op : ops[each]) {
      int exp, type, node, q_id;
      tie(exp, type, node, q_id) = op;
      if (type == 0) {
        int l = st[node];
        int r = en[node];
        update(l, exp, val_bit);
        update(r + 1, -exp, val_bit);
        update(l, 1, less_bit);
        update(r + 1, -1, less_bit);
      } else {
        int idx = st[node];
        int non_zeros = query(idx, zero_bit);
        int less = query(idx, less_bit);
        int more = non_zeros - less;
        int total_exp = query(idx, val_bit) + more * abs(exp);
        if (exp < 0) {
          total_exp *= -1;
        }
        ans[q_id] =
            mult(ans[q_id], total_exp < 0 ? inv(f_exp(each, abs(total_exp)))
                                          : f_exp(each, total_exp));
      }
    }
    for (auto &op : ops[each]) {
      int exp, type, node, q_id;
      tie(exp, type, node, q_id) = op;
      if (type == 0) {
        int l = st[node];
        int r = en[node];
        update(l, -exp, val_bit);
        update(r + 1, exp, val_bit);
        update(l, -1, less_bit);
        update(r + 1, 1, less_bit);
        update(l, -1, zero_bit);
        update(r + 1, 1, zero_bit);
      }
    }
  }
  for (int i = int(0); i < int(q); i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}