#include <bits/stdc++.h>
using namespace std;
const int PR = 10000007;
int isp[PR], lsp[PR];
void initPR() {
  for (int e = 0; e < PR; e++) isp[e] = 1;
  isp[0] = 0;
  isp[1] = 0;
  for (int e = 2; e < PR; e++) {
    if (isp[e]) {
      lsp[e] = e;
      for (int f = 2 * e; f < PR; f += e) {
        if (lsp[f] == 0) lsp[f] = e;
        isp[f] = 0;
      }
    }
  }
}
vector<pair<int, int> > factors;
void push(int w) {
  factors.clear();
  while (w != 1) {
    if (factors.empty() || factors.back().first != lsp[w])
      factors.push_back({lsp[w], 1});
    else
      factors.back().second++;
    w /= lsp[w];
  }
}
const int mod = 1000000007;
int exponentiation(int a, long long b) {
  int ans = 1;
  for (; b; a = (1ll * a * a) % mod, b >>= 1)
    if (b & 1) ans = (1ll * ans * a) % mod;
  return ans;
}
int gcd(int A, int B) {
  if (B == 0) return A;
  return gcd(B, A % B);
}
const int lg = 24;
const int maxn = 100100;
int lca[lg][maxn], dep[maxn];
map<int, map<int, int> > cnt;
int ans[maxn];
int a[maxn];
void upd(int p) {
  for (auto e : factors) cnt[e.first][e.second] += p;
}
struct event {
  int w, pw, to;
};
vector<event> r[maxn];
vector<int> g[maxn];
int getLca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  for (int e = lg - 1; e >= 0; e--)
    if ((dep[u] - dep[v]) & (1 << e)) u = lca[e][u];
  if (u == v) return u;
  for (int e = lg - 1; e >= 0; e--)
    if (lca[e][u] != lca[e][v]) u = lca[e][u], v = lca[e][v];
  return lca[0][u];
}
void init(int cur, int p) {
  lca[0][cur] = p;
  for (int e = 1; e < lg; e++)
    if (lca[e - 1][cur]) lca[e][cur] = lca[e - 1][lca[e - 1][cur]];
  for (int nxt : g[cur]) {
    if (nxt != p) {
      dep[nxt] = dep[cur] + 1;
      init(nxt, cur);
    }
  }
}
void solve(int cur, int p) {
  push(a[cur]);
  upd(1);
  for (auto e : r[cur]) {
    push(e.w);
    int now = 1;
    for (auto fac : factors) {
      int sum = 0, qtd = 0;
      for (int f = 0; f <= fac.second; f++) sum += f * cnt[fac.first][f];
      for (int f = fac.second + 1; f < lg; f++) qtd += cnt[fac.first][f];
      now = 1ll * now * exponentiation(fac.first, sum) % mod;
      now = 1ll * now * exponentiation(fac.first, qtd * fac.second) % mod;
    }
    now = exponentiation(now, e.pw);
    ans[e.to] = 1ll * ans[e.to] * now % mod;
  }
  for (int nxt : g[cur]) {
    if (nxt != p) {
      solve(nxt, cur);
    }
  }
  push(a[cur]);
  upd(-1);
}
int u[maxn], v[maxn], lc[maxn];
int main() {
  initPR();
  int n;
  scanf("%d", &n);
  for (int e = 0; e < n - 1; e++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int e = 1; e <= n; e++) scanf("%d", a + e);
  init(1, 0);
  int q;
  scanf("%d", &q);
  for (int e = 0; e < q; e++) {
    int w;
    scanf("%d %d %d", u + e, v + e, &w);
    lc[e] = getLca(u[e], v[e]);
    r[lc[e]].push_back({w, 2 * mod - 4, e});
    r[u[e]].push_back({w, 1, e});
    r[v[e]].push_back({w, 1, e});
    ans[e] = gcd(w, a[lc[e]]);
  }
  solve(1, 0);
  for (int e = 0; e < q; e++) printf("%d\n", ans[e]);
  return 0;
}
