#include <bits/stdc++.h>
using std::map;
using std::pair;
using std::swap;
using std::vector;
const int N = 100010;
const int M = 10000010;
const int MOD = 1e9 + 7;
int _w;
int fpow(int a, int b) {
  int c = 1;
  while (b) {
    if (b & 1) c = int(1LL * a * c % MOD);
    a = int(1LL * a * a % MOD);
    b >>= 1;
  }
  return c;
}
int n, q, qu[N], qv[N], ans[N];
namespace G {
int head[N], nxt[N * 2], to[N * 2], eid;
void init() {
  eid = 0;
  memset(head, -1, sizeof head);
}
void link(int u, int v) {
  to[eid] = v, nxt[eid] = head[u], head[u] = eid++;
  to[eid] = u, nxt[eid] = head[v], head[v] = eid++;
}
}  // namespace G
namespace BIT {
int c[N];
int lb(int x) { return x & (-x); }
void add(int p, int x) {
  for (; p <= n; p += lb(p)) c[p] += x;
}
int sum(int p) {
  int s = 0;
  for (; p; p -= lb(p)) s += c[p];
  return s;
}
int sum(int l, int r) { return sum(r) - sum(l - 1); }
}  // namespace BIT
namespace HLD {
int dfn[N], clk, top[N], sz[N], son[N], pa[N], dep[N];
void init1(int u, int fa, int d) {
  using namespace G;
  sz[u] = 1, dep[u] = d, pa[u] = fa, son[u] = 0;
  for (int i = head[u]; ~i; i = nxt[i]) {
    int v = to[i];
    if (fa == v) continue;
    init1(v, u, d + 1);
    sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) son[u] = v;
  }
}
void init2(int u, int tp) {
  using namespace G;
  dfn[u] = ++clk, top[u] = tp;
  if (son[u]) init2(son[u], tp);
  for (int i = head[u]; ~i; i = nxt[i]) {
    int v = to[i];
    if (v == pa[u] || v == son[u]) continue;
    init2(v, v);
  }
}
void init() {
  clk = 0;
  init1(1, 0, 1);
  init2(1, 1);
}
int query(int u, int v) {
  int s = 0;
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]]) swap(u, v);
    s += BIT::sum(dfn[top[u]], dfn[u]);
    u = pa[top[u]];
  }
  if (dep[u] > dep[v]) swap(u, v);
  s += BIT::sum(dfn[u], dfn[v]);
  return s;
}
void add(int u, int x) { BIT::add(dfn[u], x); }
}  // namespace HLD
int isp[M], prm[M], pn;
void prelude() {
  memset(isp, 1, sizeof isp);
  isp[0] = isp[1] = 0;
  for (int i = 2; i < M; ++i)
    if (isp[i])
      for (long long j = 1LL * i * i; j < M; j += i) isp[j] = 0;
  for (int i = 2; i < M; ++i)
    if (isp[i]) prm[pn++] = i;
}
map<int, vector<pair<int, int> > > fac, qry;
void solve() {
  for (map<int, vector<pair<int, int> > >::iterator it = fac.begin();
       it != fac.end(); ++it) {
    int p = it->first;
    if (!qry.count(p)) continue;
    vector<pair<int, int> > &qvec = qry[p], &vec = it->second;
    sort(qvec.begin(), qvec.end());
    sort(vec.begin(), vec.end());
    vector<pair<int, int> >::iterator qit = qvec.begin();
    for (vector<pair<int, int> >::iterator vit = vec.begin(); vit != vec.end();
         ++vit) {
      while (qit != qvec.end() && vit->first > qit->first) {
        int id = qit->second;
        int cnt = HLD::query(qu[id], qv[id]);
        ans[id] = int(1LL * ans[id] * fpow(p, cnt) % MOD);
        ++qit;
      }
      HLD::add(vit->second, 1);
    }
    while (qit != qvec.end()) {
      int id = qit->second;
      int cnt = HLD::query(qu[id], qv[id]);
      ans[id] = int(1LL * ans[id] * fpow(p, cnt) % MOD);
      ++qit;
    }
    for (vector<pair<int, int> >::iterator vit = vec.begin(); vit != vec.end();
         ++vit)
      HLD::add(vit->second, -1);
  }
}
int main() {
  _w = scanf("%d", &n);
  G::init();
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    _w = scanf("%d%d", &u, &v);
    G::link(u, v);
  }
  HLD::init();
  prelude();
  for (int i = 1; i <= n; ++i) {
    int a;
    _w = scanf("%d", &a);
    for (int j = 0; prm[j] * prm[j] <= a; ++j) {
      int x = prm[j], cnt = 0;
      while (a % x == 0) {
        fac[x].push_back(pair<int, int>(++cnt, i));
        a /= x;
      }
    }
    if (a != 1) fac[a].push_back(pair<int, int>(1, i));
  }
  _w = scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    _w = scanf("%d%d", qu + i, qv + i);
    int x;
    _w = scanf("%d", &x);
    for (int j = 0; prm[j] * prm[j] <= x; ++j) {
      int y = prm[j], cnt = 0;
      while (x % y == 0) ++cnt, x /= y;
      if (cnt) qry[y].push_back(pair<int, int>(cnt, i));
    }
    if (x != 1) qry[x].push_back(pair<int, int>(1, i));
    ans[i] = 1;
  }
  solve();
  for (int i = 0; i < q; ++i) printf("%d\n", ans[i]);
  return 0;
}
