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 Imx = 2147483647;
const long long Lbig = 2e18;
const int mod = 1e9 + 7;
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len) return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\n') c = get();
    if (it > 0) it--;
    return c != EOF;
  }
} _buff;
inline long long getnum() {
  long long r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
  if (c == '-') ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
  if (x < 0) putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c <= 32) c = _buff.get();
  return c;
}
long long qpow(long long x, long long k) {
  return k == 0 ? 1
                : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod;
}
int n, ANS[100111];
vector<int> con[100111];
int dfn[100111], dfnr[100111], dtot, lv[100111], fa[20][100111], a[100111];
const int BITsz = 100000 + 10;
struct BITadd {
  long long a[BITsz];
  int tag[BITsz], ctag;
  void init() {
    memset(tag, 0, sizeof(tag));
    ctag = 1;
  }
  BITadd() { init(); }
  void add(int p, long long x) {
    assert(p > 0);
    for (int i = p; i < BITsz; i += i & (-i)) {
      if (tag[i] != ctag) tag[i] = ctag, a[i] = 0;
      a[i] = a[i] + x;
    }
  }
  long long query(int p) {
    long long ans = 0;
    for (int i = p; i > 0; i -= i & (-i)) {
      if (tag[i] != ctag) tag[i] = ctag, a[i] = 0;
      ans = ans + a[i];
    }
    return ans;
  }
  void addseg(int l, int r, long long x) {
    add(l, x);
    add(r + 1, -x);
  }
  void clear() { ctag++; }
} bit;
void pdfs(int x) {
  dfn[x] = ++dtot;
  for (int i = 1; i < 20; i++) fa[i][x] = fa[i - 1][fa[i - 1][x]];
  for (int i = 0; i < con[x].size(); i++) {
    int u = con[x][i];
    if (u == fa[0][x]) continue;
    fa[0][u] = x;
    lv[u] = lv[x] + 1;
    pdfs(u);
  }
  dfnr[x] = dtot;
}
void init_tree() {
  lv[1] = 1;
  pdfs(1);
}
int get_lca(int x, int y) {
  if (lv[x] > lv[y]) swap(x, y);
  for (int i = 19; i >= 0; i--)
    if ((lv[y] - lv[x]) >= (1 << i)) y = fa[i][y];
  if (x == y) return x;
  for (int i = 19; i >= 0; i--)
    if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
  return fa[0][x];
}
vector<pair<int, int> > factorize(int x) {
  vector<pair<int, int> > v;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      int cnt = 0;
      while (x % i == 0) x /= i, cnt++;
      v.push_back(make_pair(i, cnt));
    }
  }
  if (x > 1) v.push_back(make_pair(x, 1));
  return v;
}
map<int, int> prid;
int prtot, ptab[1000111];
int getprid(int pr) {
  int &v = prid[pr];
  if (v == 0) v = ++prtot, ptab[prtot] = pr;
  return v;
}
int pu[100111], pv[100111], pp[100111];
vector<pair<int, int> > cmd[1000111];
vector<pair<int, int> > qr[1000111];
void solve() {
  static long long pre_ans[100111];
  for (int i = 1; i <= prtot; i++) {
    bit.clear();
    sort(cmd[i].begin(), cmd[i].end());
    reverse(cmd[i].begin(), cmd[i].end());
    sort(qr[i].begin(), qr[i].end());
    reverse(qr[i].begin(), qr[i].end());
    int it = 0;
    for (int j = 0; j < qr[i].size(); j++) {
      int curp = qr[i][j].first;
      while (it < cmd[i].size() && cmd[i][it].first >= curp) {
        int x = cmd[i][it].second;
        bit.addseg(dfn[x], dfnr[x], 1);
        it++;
      }
      int id = qr[i][j].second;
      long long cur_ans = bit.query(dfn[pu[id]]) + bit.query(dfn[pv[id]]) -
                          bit.query(dfn[pp[id]]) -
                          bit.query(dfn[fa[0][pp[id]]]);
      ANS[id] = 1ll * ANS[id] *
                qpow(ptab[i], 1ll * curp * (cur_ans - pre_ans[id])) % mod;
      pre_ans[id] = cur_ans;
    }
    for (int j = 0; j < qr[i].size(); j++) pre_ans[qr[i][j].second] = 0;
  }
}
int main() {
  n = getnum();
  for (int i = 1; i < n; i++) {
    int x, y;
    x = getnum(), y = getnum();
    con[x].push_back(y);
    con[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    a[i] = getnum();
    vector<pair<int, int> > v = factorize(a[i]);
    for (int j = 0; j < v.size(); j++) {
      int id = getprid(v[j].first);
      cmd[id].push_back(make_pair(v[j].second, i));
    }
  }
  init_tree();
  int q;
  q = getnum();
  for (int i = 1; i <= q; i++) {
    int u, v, x;
    u = getnum(), v = getnum(), x = getnum();
    int p = get_lca(u, v);
    pu[i] = u;
    pv[i] = v;
    pp[i] = p;
    vector<pair<int, int> > fac = factorize(x);
    for (int j = 0; j < fac.size(); j++) {
      if (prid.find(fac[j].first) == prid.end()) continue;
      int id = getprid(fac[j].first);
      for (int t = 1; t <= fac[j].second; t++)
        qr[id].push_back(make_pair(t, i));
    }
  }
  for (int i = 1; i <= q; i++) ANS[i] = 1;
  solve();
  for (int i = 1; i <= q; i++) putnum(ANS[i]), putchar('\n');
  return 0;
}