#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  bool f = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = 1;
  for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
  if (f) x = -x;
}
template <typename F>
inline void write(F x, char ed = '\n') {
  static short st[30];
  short tp = 0;
  if (x < 0) putchar('-'), x = -x;
  do st[++tp] = x % 10, x /= 10;
  while (x);
  while (tp) putchar('0' | st[tp--]);
  putchar(ed);
}
template <typename T>
inline void write(T *st, int n, string s = "") {
  if (s.size()) cout << s << ": ";
  while (n--) write(*st++, ' ');
  puts("");
}
const int N = 100500;
const int M = 10000500;
const int P = 1e9 + 7;
int ne[N << 1], to[N << 1], h[N], dfn[N], Top[N], son[N], siz[N], id[N], num,
    dep[N], tot;
inline void add(int x, int y) { ne[++tot] = h[x], to[h[x] = tot] = y; }
long long fpw(long long x, long long mi) {
  long long res = 1;
  for (; mi; mi >>= 1, x = x * x % P)
    if (mi & 1) res = res * x % P;
  return res;
}
long long ans[N];
int f[N], pr[M], e[M], val[N], totp;
int d[M], len[M], st[M], nw = 0, n, q;
void prework(int lim) {
  for (int i = 2; i <= lim; ++i) {
    if (!e[i]) pr[++totp] = e[i] = i;
    if (e[i] == i) {
      st[i] = nw;
      int k = lim;
      while (k) ++nw, ++len[i], k /= i;
    }
    for (int j = 1; j <= totp && pr[j] * i <= lim; ++j) {
      e[pr[j] * i] = pr[j];
      if (pr[j] == e[i]) break;
    }
  }
}
void dfs1(int x, int fa) {
  dep[x] = dep[f[x] = fa] + (siz[x] = 1);
  for (int i = h[x], y; i; i = ne[i]) {
    if ((y = to[i]) == fa) continue;
    dfs1(y, x), siz[x] += siz[y];
    if (siz[y] > siz[son[x]]) son[x] = y;
  }
}
void dfs2(int x, int topf) {
  Top[x] = topf, id[dfn[x] = ++num] = x;
  if (son[x]) dfs2(son[x], topf);
  for (int i = h[x], y; i; i = ne[i])
    if (!dfn[y = to[i]]) dfs2(y, y);
}
int Lca(int x, int y) {
  while (Top[x] != Top[y]) {
    if (dep[Top[x]] < dep[Top[y]]) swap(x, y);
    x = f[Top[x]];
  }
  return dep[x] < dep[y] ? x : y;
}
vector<pair<int, int> > qry[N];
void getans(int x, int fa) {
  int tx = val[x];
  while (tx != 1) {
    int p = e[tx], mi = 0;
    while (tx % p == 0) tx /= p, ++mi;
    for (int i = 1; i <= mi; ++i) d[st[p] + i - 1] += i;
    for (int i = mi + 1; i <= len[p]; ++i) d[st[p] + i - 1] += mi;
  }
  for (auto t : qry[x]) {
    tx = t.second;
    long long mul = 1;
    while (tx != 1) {
      int p = e[tx], mi = 0;
      while (tx % p == 0) tx /= p, ++mi;
      mul = mul * fpw(p, d[st[p] + mi - 1]) % P;
    }
    if (t.first > 0)
      ans[t.first] = ans[t.first] * mul % P;
    else
      ans[-t.first] = ans[-t.first] * fpw(mul, P - 2) % P;
  }
  for (int i = h[x]; i; i = ne[i])
    if (to[i] != fa) getans(to[i], x);
  tx = val[x];
  while (tx != 1) {
    int p = e[tx], mi = 0;
    while (tx % p == 0) tx /= p, ++mi;
    for (int i = 1; i <= mi; ++i) d[st[p] + i - 1] -= i;
    for (int i = mi + 1; i <= len[p]; ++i) d[st[p] + i - 1] -= mi;
  }
}
int main() {
  read(n);
  for (int i = 1, x, y; i < n; ++i) read(x), read(y), add(x, y), add(y, x);
  dfs1(1, 0), dfs2(1, 1), prework(1e7);
  for (int i = 1; i <= n; ++i) read(val[i]);
  read(q);
  for (int i = 1, x, y, t; i <= q; ++i) {
    read(x), read(y), read(t), ans[i] = 1;
    qry[x].push_back(make_pair(i, t));
    qry[y].push_back(make_pair(i, t));
    qry[Lca(x, y)].push_back(make_pair(-i, t));
    qry[f[Lca(x, y)]].push_back(make_pair(-i, t));
  }
  getans(1, 0);
  for (int i = 1; i <= q; ++i) write(ans[i]);
  return 0;
}
