#include <bits/stdc++.h>
using namespace std;
inline int gi() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
  return sum;
}
const int maxn = 100005, mod = 1e9 + 7, maxv = 1e7 + 5, S = sqrt(1e7);
int n, q, val[maxn], ans[maxn];
int p[maxv], pcnt, d[maxv];
vector<pair<int, int>> Inc[maxn], Dec[maxn];
int tmp[maxv * 2], *g[maxv], Log[maxv];
struct edge {
  int to, next;
} e[maxn * 2];
int h[maxn], tot;
int dep[maxn], siz[maxn], son[maxn], top[maxn], fa[maxn];
void pre(int n = 1e7) {
  for (int i = 2; i <= n; ++i) {
    if (!d[i]) p[++pcnt] = i, d[i] = i;
    for (int j = 1; j <= pcnt && i * p[j] <= n; ++j) {
      d[i * p[j]] = p[j];
      if (i % p[j] == 0) break;
    }
  }
}
inline void add(int u, int v) {
  e[++tot] = (edge){v, h[u]};
  h[u] = tot;
  e[++tot] = (edge){u, h[v]};
  h[v] = tot;
}
void dfs1(int u) {
  siz[u] = 1;
  dep[u] = dep[fa[u]] + 1;
  for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
    if (v != fa[u]) {
      fa[v] = u;
      dfs1(v);
      siz[u] += siz[v];
      if (siz[v] > siz[son[u]]) son[u] = v;
    }
}
void dfs2(int u) {
  if (son[u]) top[son[u]] = top[u], dfs2(son[u]);
  for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
    if (v != fa[u] && v != son[u]) top[v] = v, dfs2(v);
}
int lca(int u, int v) {
  while (top[u] != top[v]) {
    if (dep[top[u]] > dep[top[v]])
      u = fa[top[u]];
    else
      v = fa[top[v]];
  }
  return dep[u] < dep[v] ? u : v;
}
inline int fpow(int x, int k) {
  int res = 1;
  while (k) {
    if (k & 1) res = (long long)res * x % mod;
    k >>= 1;
    x = (long long)x * x % mod;
  }
  return res;
}
inline void insert(int x, int v) {
  static int p, cnt;
  while (x > 1) {
    p = d[x];
    cnt = 0;
    while (d[x] == p) ++cnt, x /= d[x];
    for (int i = 0; i < cnt - 1; ++i) g[p][i] += (i + 1) * v;
    for (int i = cnt - 1; i < Log[p]; ++i) g[p][i] += cnt * v;
  }
}
inline int query(int x) {
  static int p, cnt;
  int res = 1;
  while (x > 1) {
    p = d[x];
    cnt = 0;
    while (d[x] == p) ++cnt, x /= d[x];
    res = (long long)res * fpow(p, g[p][cnt - 1]) % mod;
  }
  return res;
}
inline void dfs(int u) {
  insert(val[u], 1);
  for (auto t : Inc[u])
    ans[t.second] = (long long)ans[t.second] * query(t.first) % mod;
  for (auto t : Dec[u])
    ans[t.second] =
        (long long)ans[t.second] * fpow(query(t.first), mod - 2) % mod;
  for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
    if (v != fa[u]) dfs(v);
  insert(val[u], -1);
}
int main() {
  n = gi();
  for (int i = 1; i < n; ++i) add(gi(), gi());
  for (int i = 1; i <= n; ++i) val[i] = gi();
  pre();
  dfs1(1);
  top[1] = 1;
  dfs2(1);
  q = gi();
  for (int x, y, z, w, i = 1; i <= q; ++i) {
    x = gi();
    y = gi();
    z = lca(x, y);
    w = gi();
    Inc[x].push_back(make_pair(w, i));
    Inc[y].push_back(make_pair(w, i));
    Dec[z].push_back(make_pair(w, i));
    Dec[fa[z]].push_back(make_pair(w, i));
    ans[i] = 1;
  }
  int *now = tmp;
  for (int i = 1; i <= pcnt; ++i) {
    g[p[i]] = now;
    for (int j = 1; (long long)j * p[i] < maxv; j *= p[i]) ++Log[p[i]];
    now += Log[p[i]] + 1;
  }
  dfs(1);
  for (int i = 1; i <= q; ++i) printf("%d\n", ans[i]);
  return 0;
}
