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 Inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
template <int jt>
struct ModInt {
  int x;
  ModInt(int v = 0) {
    x = v;
    if (x >= jt || x < 0) {
      x %= jt;
      if (x < 0) x += jt;
    }
  }
  ModInt(long long v) {
    x = v % jt;
    if (x < 0) x += jt;
  }
  operator int() const { return x; }
  friend inline ModInt &operator+=(ModInt &a, const ModInt &b) {
    a.x += b.x;
    if (a.x >= jt) a.x -= jt;
    return a;
  }
  friend inline ModInt operator+(ModInt a, const ModInt &b) { return a += b; }
  friend inline ModInt operator+(ModInt a, int b) { return a += ModInt(b); }
  friend inline ModInt operator+(int a, ModInt b) { return b += ModInt(a); }
  friend inline ModInt &operator++(ModInt &a) { return a += 1; }
  friend inline ModInt &operator-=(ModInt &a, const ModInt &b) {
    a.x -= b.x;
    if (a.x < 0) a.x += jt;
    return a;
  }
  friend inline ModInt operator-(ModInt a, const ModInt &b) { return a -= b; }
  friend inline ModInt operator-(ModInt a, int b) { return a -= ModInt(b); }
  friend inline ModInt operator-(int a, ModInt b) { return ModInt(a) - b; }
  friend inline ModInt &operator--(ModInt &a) { return a -= 1; }
  friend inline ModInt &operator*=(ModInt &a, const ModInt &b) {
    a.x = (long long)a.x * b.x % jt;
    return a;
  }
  friend inline ModInt operator*(ModInt a, const ModInt &b) { return a *= b; }
  friend inline ModInt operator*(ModInt a, int b) { return a *= ModInt(b); }
  friend inline ModInt operator*(int a, ModInt b) { return b *= ModInt(a); }
  static inline void exgcd(long long a, long long b, long long &x,
                           long long &y) {
    b ? (exgcd(b, a % b, y, x), y -= a / b * x) : (x = 1, y = 0);
  }
  friend inline ModInt inv(const ModInt &a) {
    long long x, y;
    exgcd(a, jt, x, y);
    return x;
  }
  friend inline ModInt operator/(const ModInt &a, const ModInt &b) {
    return a * inv(b);
  }
  friend inline ModInt operator/(ModInt a, int b) { return a * inv(ModInt(b)); }
  friend inline ModInt operator/(int a, ModInt b) { return ModInt(a) * inv(b); }
  friend inline ModInt operator/=(ModInt &a, const ModInt &b) {
    return a = a / b;
  }
};
const int jt = 1000000007;
const int twt = 1e7 + 5, qwq = 1e6 + 5, iwi = 1 << 17;
int N, Q;
vector<int> nei[iwi];
int fa[20][iwi], lvl[iwi];
int a[iwi];
int sml[twt + 5], it[twt + 5], ti[qwq], mxt[qwq], cnt[qwq], ps;
int t[iwi], id1[iwi], id2[iwi], tm1, tm2;
void inline init() {
  for (int i = (2); i <= (twt); ++i)
    if (!sml[i]) {
      ti[++ps] = i;
      it[i] = ps;
      for (int j = (i); ((i) > 0 ? j <= (twt) : j >= (twt)); j += (i))
        if (!sml[j]) {
          sml[j] = i;
        }
    }
}
void inline dfs(int now, int lst) {
  fa[0][now] = lst;
  lvl[now] = lvl[lst] + 1;
  t[now] = ++tm1;
  for (int i = 0; i < (((int)(nei[now]).size())); ++i) {
    int to = nei[now][i];
    if (to == lst) continue;
    dfs(to, now);
  }
  id1[now] = tm1;
  id2[now] = ++tm2;
}
int inline lca(int a, int b) {
  if (lvl[a] < lvl[b]) swap(a, b);
  int up = lvl[a] - lvl[b];
  while (up) a = fa[__builtin_ctz(up)][a], up &= up - 1;
  if (a == b) return a;
  for (int i = (19); ((-1) > 0 ? i <= (0) : i >= (0)); i += (-1))
    if (fa[i][a] != fa[i][b]) {
      a = fa[i][a], b = fa[i][b];
    }
  return fa[0][a];
}
vector<pair<int, int> > hvt[qwq], hvq[qwq];
int sm[iwi][4];
ModInt<jt> ans[iwi];
vector<ModInt<jt> > mii[qwq];
struct Fenwick {
  int dt[iwi];
  void inline add(int a, int x) {
    for (; a < iwi; a += a & -a) dt[a] += x;
  }
  int inline sum(int a) {
    int res = 0;
    for (; a; a -= a & -a) res += dt[a];
    return res;
  }
} b1, b2;
void inline Add(int a, int x) {
  b1.add(t[a], x);
  b2.add(id2[a], x);
}
int inline Get(int a) {
  if (!a) return 0;
  return b1.sum(id1[a]) - b2.sum(id2[a] - 1);
}
int main() {
  init();
  scanf("%d", &N);
  for (int i = 1; i <= (N - 1); ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    nei[a].push_back(b);
    nei[b].push_back(a);
  }
  dfs(1, 0);
  for (int i = 1; i <= (19); ++i)
    for (int j = 1; j <= (N); ++j) fa[i][j] = fa[i - 1][fa[i - 1][j]];
  for (int i = 1; i <= (N); ++i) {
    scanf("%d", a + i);
    int x = a[i];
    while (x != 1) {
      int p = sml[x];
      int tms = 0;
      while (x % p == 0) {
        x /= p;
        ++tms;
      }
      hvt[it[p]].push_back(make_pair(i, tms));
      cnt[it[p]] += tms;
    }
  }
  for (int p = 1; p <= (ps); ++p) {
    mii[p].push_back(1);
    for (int i = 1; i <= (cnt[p]); ++i) {
      mii[p].push_back(mii[p].back() * ti[p]);
    }
  }
  scanf("%d", &Q);
  for (int i = 1; i <= (Q); ++i) {
    int u, v, x;
    scanf("%d%d%d", &u, &v, &x);
    int c = lca(u, v);
    sm[i][0] = u;
    sm[i][1] = v;
    sm[i][2] = c;
    sm[i][3] = fa[0][c];
    while (x != 1) {
      int p = sml[x];
      int tms = 0;
      while (x % p == 0) {
        x /= p;
        ++tms;
      }
      hvq[it[p]].push_back(make_pair(tms, i));
      mxt[it[p]] = max(mxt[it[p]], tms);
    }
    ans[i] = 1;
  }
  for (int p = 1; p <= (ps); ++p) {
    sort((hvq[p]).begin(), (hvq[p]).end());
    int pi = ti[p];
    int ptr = 0;
    for (int t = 1; t <= (mxt[p]); ++t) {
      for (int i = 0; i < (((int)(hvt[p]).size())); ++i) {
        if (hvt[p][i].second < t) continue;
        int to = hvt[p][i].first;
        Add(to, 1);
      }
      while (ptr < ((int)(hvq[p]).size()) && hvq[p][ptr].first == t) {
        int id = hvq[p][ptr++].second;
        int res = Get(sm[id][0]) + Get(sm[id][1]);
        res -= Get(sm[id][2]) + Get(sm[id][3]);
        ans[id] *= mii[p][res];
      }
    }
    for (int t = 1; t <= (mxt[p]); ++t)
      for (int i = 0; i < (((int)(hvt[p]).size())); ++i) {
        if (hvt[p][i].second < t) continue;
        int to = hvt[p][i].first;
        Add(to, -1);
      }
  }
  for (int i = 1; i <= (Q); ++i) printf("%d\n", (int)ans[i]);
  return 0;
}