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;
template <typename T>
bool chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}
template <typename T>
bool chkmin(T &x, T y) {
  return x > y ? x = y, true : false;
}
int readint() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
struct node {
  int opt, pt, val;
  bool operator<(const node c) const { return val > c.val; }
  node(int opt = 0, int pt = 0, int val = 0) : opt(opt), pt(pt), val(val) {}
};
const int cys = 1000000007;
int n, m, tot, ncnt, pcnt;
int v[200005], nxt[200005], h[100005], dfn[100005], siz[100005], f[100005][20],
    x[100005], y[100005], z[100005], now[100005];
int prm[1000005], p0[10000005], dep[100005];
long long ans[100005];
bool fl[10000005];
vector<pair<int, int> > vec[1000005], qry[1000005];
struct bit {
  int timer;
  int c[100005], mark[100005];
  void add(int x, int v) {
    for (; x <= n; x += (x & (-x))) {
      if (mark[x] != timer) mark[x] = timer, c[x] = 0;
      c[x] += v;
    }
  }
  int ask(int x) {
    int ret = 0;
    for (; x; x -= (x & (-x))) ret += (mark[x] == timer ? c[x] : 0);
    return ret;
  }
} T[2];
long long qpow(long long x, long long p) {
  long long ret = 1;
  for (; p; p >>= 1, x = x * x % cys)
    if (p & 1) ret = ret * x % cys;
  return ret;
}
void getprime(int m) {
  fl[1] = 1;
  for (int i = 2; i <= m; i++) {
    if (!fl[i]) prm[++pcnt] = i, p0[i] = pcnt;
    for (int j = 1; j <= pcnt && i * prm[j] <= m; j++) {
      fl[i * prm[j]] = 1, p0[i * prm[j]] = j;
      if (i % prm[j] == 0) break;
    }
  }
}
int getnum(int x, int y, int l) {
  return T[0].ask(dfn[x]) + T[0].ask(dfn[y]) - T[0].ask(dfn[l]) -
         T[0].ask(dfn[f[l][0]]);
}
int getsum(int x, int y, int l) {
  return T[1].ask(dfn[x]) + T[1].ask(dfn[y]) - T[1].ask(dfn[l]) -
         T[1].ask(dfn[f[l][0]]);
}
void change(int x, int y) {
  T[0].add(dfn[x], 1), T[1].add(dfn[x], y);
  if (dfn[x] + siz[x] <= n)
    T[0].add(dfn[x] + siz[x], -1), T[1].add(dfn[x] + siz[x], -y);
}
void addedge(int x, int y) {
  v[++tot] = y;
  nxt[tot] = h[x];
  h[x] = tot;
  v[++tot] = x;
  nxt[tot] = h[y];
  h[y] = tot;
}
void dfs(int u, int fa) {
  dfn[u] = ++ncnt, siz[u] = 1;
  for (int i = 1; i <= 19; i++) f[u][i] = f[f[u][i - 1]][i - 1];
  for (int p = h[u]; p; p = nxt[p]) {
    if (v[p] == fa) continue;
    f[v[p]][0] = u, dep[v[p]] = dep[u] + 1;
    dfs(v[p], u);
    siz[u] += siz[v[p]];
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 19; i >= 0; i--)
    if (dep[f[x][i]] >= dep[y]) x = f[x][i];
  if (x == y) return x;
  for (int i = 19; i >= 0; i--)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
int main() {
  getprime(10000000);
  n = readint();
  for (int i = 1; i < n; i++) addedge(readint(), readint());
  dep[1] = 1;
  dfs(1, -1);
  for (int i = 1; i <= n; i++) {
    int tmp = readint();
    while (tmp > 1) {
      int t = p0[tmp], num = 0;
      while (tmp % prm[t] == 0) tmp /= prm[t], num++;
      vec[t].push_back(make_pair(i, num));
    }
  }
  m = readint();
  for (int i = 1; i <= m; i++) {
    x[i] = readint();
    y[i] = readint();
    z[i] = lca(x[i], y[i]);
    int tmp = readint();
    while (tmp > 1) {
      int t = p0[tmp], num = 0;
      while (tmp % prm[t] == 0) tmp /= prm[t], num++;
      qry[t].push_back(make_pair(i, num));
    }
  }
  for (int i = 1; i <= m; i++) ans[i] = 1;
  for (int i = 1; i <= pcnt; i++) {
    if (!vec[i].size() && !qry[i].size()) continue;
    vector<node> cur(0);
    for (auto r : vec[i]) cur.push_back(node(0, r.first, r.second));
    for (auto r : qry[i]) cur.push_back(node(1, r.first, r.second));
    sort(cur.begin(), cur.end());
    T[0].timer++, T[1].timer++;
    for (auto r : cur) {
      if (r.opt == 1)
        now[r.pt] += r.val * getnum(x[r.pt], y[r.pt], z[r.pt]) -
                     getsum(x[r.pt], y[r.pt], z[r.pt]);
      else
        change(r.pt, r.val);
    }
    for (auto r : qry[i])
      ans[r.first] = ans[r.first] *
                     qpow(prm[i], now[r.first] + getsum(x[r.first], y[r.first],
                                                        z[r.first])) %
                     cys,
      now[r.first] = 0;
  }
  for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]);
  return 0;
}