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 maxn = 1e5 + 10;
const int maxm = 1e7 + 2;
const int maxt = 1e7 + 2;
const int B = 1000000;
bool np[maxm];
vector<int> ps;
map<int, vector<pair<int, int> > > cnode, cquery;
void getprime() {
  for (int i = 2; i < maxm; i++) {
    if (!np[i]) ps.push_back(i);
    for (int j = 0; j < ps.size() && i * 1ll * ps[j] < maxm; j++) {
      np[i * ps[j]] = 1;
      if (i % ps[j] == 0) break;
    }
  }
}
vector<int> g[maxn];
int a[maxn], qu[maxn], qv[maxn], qx[maxn], qlca[maxn], ans[maxn];
vector<pair<int, int> > rec[maxn], deq[maxn];
vector<int> anq[maxn];
int n, m, q;
void decompose(int val, vector<pair<int, int> > &v) {
  for (int i = 0; i < ps.size() && ps[i] * 1ll * ps[i] <= val; i++) {
    if (val % ps[i]) continue;
    int c = 0;
    while (val % ps[i] == 0) {
      c++;
      val /= ps[i];
    }
    v.push_back(make_pair(ps[i], c));
  }
  if (val != 1) v.push_back(make_pair(val, 1));
}
int dfn[maxn], dout[maxn], dep[maxn], DFN;
int pa[19][maxn];
void dfs(int x, int fa) {
  dfn[x] = ++DFN;
  dep[x] = dep[fa] + 1;
  pa[0][x] = fa;
  for (int i = 0; i < g[x].size(); i++) {
    if (g[x][i] != fa) dfs(g[x][i], x);
  }
  dout[x] = DFN;
}
inline int getlca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 0; i < 19; i++)
    if ((dep[x] - dep[y]) >> i & 1) x = pa[i][x];
  if (x == y) return x;
  for (int i = 18; i >= 0; i--)
    if (pa[i][x] != pa[i][y]) x = pa[i][x], y = pa[i][y];
  return pa[0][x];
}
inline bool onpath(int x, int u, int v) {
  return dfn[x] <= dfn[u] && dfn[u] <= dout[x] && dfn[v] <= dfn[x] &&
         dfn[x] <= dout[v];
}
inline int quickpow(long long base, long long ind) {
  long long ans = 1;
  while (ind > 0) {
    if (ind & 1) ans = ans * 1ll * base % 1000000007;
    ind >>= 1;
    base = base * 1ll * base % 1000000007;
  }
  return ans;
}
inline void mul(int &a, int b) { a = a * 1ll * b % 1000000007; }
struct Event {
  int a, x, val, tp, id, idpos;
  Event() {}
  Event(int a, int x, int val, int tp, int id, int idpos)
      : a(a), x(x), val(val), tp(tp), id(id), idpos(idpos) {}
  bool operator<(const Event &t) const {
    if (a != t.a) return a < t.a;
    return tp != 0 && t.tp == 0;
  }
};
vector<Event> yy, tt;
int bit[110], bit2[110];
inline int sum(int x) {
  int ret = 0;
  while (x > 0) {
    ret += bit[x];
    x -= x & -x;
  }
  return ret;
}
inline int sum2(int x) {
  int ret = 0;
  while (x > 0) {
    ret += bit2[x];
    x -= x & -x;
  }
  return ret;
}
inline void add(int x, int v) {
  while (x < 110) {
    bit[x] += v;
    x += x & -x;
  }
}
inline void add2(int x, int v) {
  while (x < 110) {
    bit2[x] += v;
    x += x & -x;
  }
}
void solve(int l, int r) {
  if (l == r) return;
  int mid = (l + r) >> 1;
  solve(l, mid);
  solve(mid + 1, r);
  int i = l, j = mid + 1;
  tt.clear();
  int cntall = 0;
  while (i <= mid && j <= r) {
    if (yy[i].x < yy[j].x || (yy[i].x == yy[j].x && yy[i].tp < yy[j].tp)) {
      if (yy[i].tp != 0) {
        int qid = yy[i].id, qpos = yy[i].idpos, qval = yy[i].val;
        anq[qid][qpos] += yy[i].tp * sum(qval);
        anq[qid][qpos] += yy[i].tp * qval * (cntall - sum2(qval));
      }
      tt.push_back(yy[i]);
      i++;
    } else {
      if (yy[j].tp == 0) {
        add(yy[j].val, yy[j].val);
        add2(yy[j].val, 1);
        cntall++;
      }
      tt.push_back(yy[j]);
      j++;
    }
  }
  while (i <= mid) {
    if (yy[i].tp != 0) {
      int qid = yy[i].id, qpos = yy[i].idpos, qval = yy[i].val;
      anq[qid][qpos] += yy[i].tp * sum(qval);
      anq[qid][qpos] += yy[i].tp * qval * (cntall - sum2(qval));
    }
    tt.push_back(yy[i]);
    i++;
  }
  while (j <= r) {
    if (yy[j].tp == 0) {
      add(yy[j].val, yy[j].val);
      add2(yy[j].val, 1);
      cntall++;
    }
    tt.push_back(yy[j]);
    j++;
  }
  if (cntall > 10) {
    memset(bit, 0, sizeof(bit));
    memset(bit2, 0, sizeof(bit2));
  } else {
    for (int i = mid + 1; i <= r; i++)
      if (yy[i].tp == 0) {
        add(yy[i].val, -yy[i].val);
        add2(yy[i].val, -1);
      }
  }
  for (int i = l; i <= r; i++) yy[i] = tt[i - l];
}
int main() {
  getprime();
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 0);
  for (int k = 1; k < 19; k++) {
    for (int i = 1; i <= n; i++) {
      pa[k][i] = pa[k - 1][pa[k - 1][i]];
    }
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    decompose(a[i], rec[i]);
    for (int j = 0; j < rec[i].size(); j++) {
      if (!cnode.count(rec[i][j].first)) {
        cnode[rec[i][j].first] = vector<pair<int, int> >();
      }
      cnode[rec[i][j].first].push_back(make_pair(i, j));
    }
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d%d", &qu[i], &qv[i], &qx[i]);
    qlca[i] = getlca(qu[i], qv[i]);
    ans[i] = 1;
    decompose(qx[i], deq[i]);
    for (int j = 0; j < deq[i].size(); j++) {
      if (!cquery.count(deq[i][j].first)) {
        cquery[deq[i][j].first] = vector<pair<int, int> >();
      }
      cquery[deq[i][j].first].push_back(make_pair(i, j));
      anq[i].push_back(0);
    }
  }
  for (map<int, vector<pair<int, int> > >::iterator it = cquery.begin();
       it != cquery.end(); it++) {
    vector<pair<int, int> > &curq = it->second;
    if (!cnode.count(it->first)) continue;
    vector<pair<int, int> > &curn = cnode[it->first];
    if (curn.size() * 1ll * curq.size() <= B) {
      for (int i = 0; i < curq.size(); i++)
        for (int j = 0; j < curn.size(); j++) {
          if (onpath(curn[j].first, qu[curq[i].first], qlca[curq[i].first]) ||
              onpath(curn[j].first, qv[curq[i].first], qlca[curq[i].first])) {
            int tmp;
            anq[curq[i].first][curq[i].second] +=
                (tmp = min(deq[curq[i].first][curq[i].second].second,
                           rec[curn[j].first][curn[j].second].second));
          }
        }
    } else {
      yy.clear();
      for (int i = 0; i < curq.size(); i++) {
        yy.push_back(Event(dfn[qu[curq[i].first]], dfn[qlca[curq[i].first]],
                           deq[curq[i].first][curq[i].second].second, -1,
                           curq[i].first, curq[i].second));
        yy.push_back(Event(dfn[qu[curq[i].first]], dfn[qu[curq[i].first]],
                           deq[curq[i].first][curq[i].second].second, 1,
                           curq[i].first, curq[i].second));
        yy.push_back(Event(dfn[qv[curq[i].first]], dfn[qlca[curq[i].first]] + 1,
                           deq[curq[i].first][curq[i].second].second, -1,
                           curq[i].first, curq[i].second));
        yy.push_back(Event(dfn[qv[curq[i].first]], dfn[qv[curq[i].first]],
                           deq[curq[i].first][curq[i].second].second, 1,
                           curq[i].first, curq[i].second));
      }
      for (int i = 0; i < curn.size(); i++) {
        yy.push_back(Event(dout[curn[i].first], dfn[curn[i].first],
                           rec[curn[i].first][curn[i].second].second, 0,
                           curn[i].first, curn[i].second));
      }
      sort(yy.begin(), yy.end());
      m = yy.size();
      memset(bit, 0, sizeof(bit));
      memset(bit2, 0, sizeof(bit2));
      solve(0, m - 1);
    }
  }
  for (int i = 1; i <= q; i++) {
    for (int j = 0; j < deq[i].size(); j++) {
      mul(ans[i], quickpow(deq[i][j].first, anq[i][j]));
    }
    printf("%d\n", ans[i]);
  }
  return 0;
}