#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkmax(T &x, T y) {
  x = x > y ? x : y;
}
template <typename T>
void chkmin(T &x, T y) {
  x = x > y ? y : x;
}
const int INF = 2139062143;
template <typename T>
void read(T &x) {
  x = 0;
  bool f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = 0;
  } while (ch > '9' || ch < '0');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  x = f ? x : -x;
}
template <typename T>
void write(T x) {
  if (x < 0) x = ~x + 1, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const int N = 1e5 + 7;
const int P = 1e6 + 7;
const int M = 1e7 + 5;
const int mod = 1e9 + 7;
const int LOG = 20 + 5;
int n, m, a[N], w[N], ans[N];
vector<int> q[N];
namespace LCA {
int E, dep[N], head[N], f[N][LOG];
struct EDGE {
  int to, nxt;
} edge[N << 1];
inline void addedge(int u, int v) {
  edge[++E].to = v;
  edge[E].nxt = head[u];
  head[u] = E;
}
inline void dfs(int u, int fa) {
  dep[u] = dep[fa] + 1;
  f[u][0] = fa;
  for (int i = 1; i <= 20; i++) f[u][i] = f[f[u][i - 1]][i - 1];
  for (int i = head[u]; i; i = edge[i].nxt) {
    int v = edge[i].to;
    if (v == fa) continue;
    dfs(v, u);
  }
}
inline int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  for (int i = 20; i >= 0; i--) {
    if (dep[f[u][i]] >= dep[v]) u = f[u][i];
    if (u == v) return u;
  }
  if (u == v) return u;
  for (int i = 20; i >= 0; i--) {
    if (f[u][i] != f[v][i]) {
      u = f[u][i];
      v = f[v][i];
    }
  }
  return f[u][0];
}
}  // namespace LCA
namespace PRIME {
int cnt, prime[P];
bool vis[M];
inline void init(int NN) {
  for (int i = 2; i <= NN; i++) {
    if (!vis[i]) {
      prime[++cnt] = i;
    }
    for (int j = 1; j <= cnt && prime[j] * i <= NN; j++) {
      vis[i * prime[j]] = 1;
      if (i % prime[j] == 0) break;
    }
  }
}
}  // namespace PRIME
namespace solve {
using namespace LCA;
int cnt[P][LOG];
inline int qpow(int a, int b) {
  int ret = 1;
  while (b) {
    if (b & 1) ret = 1ll * ret * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return ret % mod;
}
inline void add(int val, int opt) {
  for (int i = 1;
       1ll * PRIME::prime[i] * PRIME::prime[i] <= val && i <= PRIME::cnt; i++) {
    if (val % PRIME::prime[i]) continue;
    int p = 0;
    while (val % PRIME::prime[i] == 0) {
      val /= PRIME::prime[i];
      p++;
    }
    cnt[i][p] += opt;
  }
  if (val != 1) {
    int pos =
        lower_bound(PRIME::prime + 1, PRIME::prime + PRIME::cnt + 1, val) -
        PRIME::prime;
    cnt[pos][1] += opt;
  }
}
inline int gcd(int x, int y) {
  if (x < y) swap(x, y);
  return y == 0 ? x : gcd(y, x % y);
}
inline int sqr(int x) { return 1ll * x * x % mod; }
inline int query(int val) {
  int ans = 1;
  for (int i = 1;
       1ll * PRIME::prime[i] * PRIME::prime[i] <= val && i <= PRIME::cnt; i++) {
    if (val % PRIME::prime[i]) continue;
    int p = 0;
    while (val % PRIME::prime[i] == 0) {
      val /= PRIME::prime[i];
      p++;
    }
    int s = 0;
    for (int j = 1; j <= p; j++) s += cnt[i][j] * j;
    for (int j = p + 1; j < LOG; j++) s += cnt[i][j] * p;
    ans = 1ll * ans * qpow(PRIME::prime[i], s) % mod;
  }
  if (val != 1) {
    int pos =
        lower_bound(PRIME::prime + 1, PRIME::prime + PRIME::cnt + 1, val) -
        PRIME::prime;
    int s = 0;
    for (int j = 1; j < LOG; j++) s += cnt[pos][j];
    ans = 1ll * ans * qpow(val, s) % mod;
  }
  return (ans % mod + mod) % mod;
}
inline void dfs(int u, int f) {
  add(a[u], 1);
  for (int i = 0, sz = q[u].size(); i < sz; i++) {
    int id = q[u][i];
    int x = query(w[abs(id)]);
    if (id > 0) ans[id] = 1ll * ans[id] % mod * x % mod;
    if (id < 0)
      ans[-id] = 1ll * ans[-id] % mod * qpow(1ll * x * x % mod, mod - 2) % mod *
                 gcd(w[-id], a[u]) % mod;
  }
  for (int i = head[u]; i; i = edge[i].nxt) {
    int v = edge[i].to;
    if (v == f) continue;
    dfs(v, u);
  }
  add(a[u], -1);
}
}  // namespace solve
int main() {
  read(n);
  PRIME::init(1e7);
  for (int i = 1, u, v; i < n; i++) {
    read(u);
    read(v);
    LCA::addedge(u, v);
    LCA::addedge(v, u);
  }
  LCA::dfs(1, 0);
  for (int i = 1; i <= n; i++) read(a[i]);
  read(m);
  for (int i = 1, x, y, z; i <= m; i++) {
    read(x);
    read(y);
    read(z);
    int LCA = LCA::lca(x, y);
    w[i] = z;
    ans[i] = 1;
    q[LCA].push_back(-i);
    q[x].push_back(i);
    q[y].push_back(i);
  }
  solve::dfs(1, 0);
  for (int i = 1; i <= m; i++) printf("%d\n", (ans[i] % mod + mod) % mod);
  return 0;
}
