#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, mo = 1e9 + 7;
const int S = 1e7 + 10;
int n, q, final[N], nex[2 * N], to[2 * N], tot, dfn[N], dfnr[N], stm, a[N];
int ans[N];
int qfinal[S], qnex[8 * N], qto[8 * N], qtot, zs[8 * N];
int ffinal[S], fnex[8 * N], fto[8 * N], ftot, fzs[8 * N];
int g[N][20], dep[N];
struct qry {
  int u, v, no, lca, w;
} qu[N];
bool cmp(qry a, qry b) { return a.w < b.w; }
void link(int x, int y) { to[++tot] = y, nex[tot] = final[x], final[x] = tot; }
int p[1000000], mf[S];
bool bz[S];
void init() {
  for (int i = 2; i < S; i++) {
    if (!bz[i]) p[++p[0]] = i, mf[i] = i;
    for (int j = 1; j <= p[0] && p[j] * i < S; j++) {
      mf[i * p[j]] = p[j];
      bz[i * p[j]] = 1;
      if (i % p[j] == 0) break;
    }
  }
}
void dfs(int x) {
  dfn[x] = ++stm;
  dep[x] = dep[g[x][0]] + 1;
  for (int i = 1; i <= 17; i++) g[x][i] = g[g[x][i - 1]][i - 1];
  for (int i = final[x]; i; i = nex[i]) {
    int y = to[i];
    if (y != g[x][0]) g[y][0] = x, dfs(y);
  }
  dfnr[x] = stm;
}
int getlca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 17; i >= 0; i--)
    if (dep[g[x][i]] >= dep[y]) x = g[x][i];
  if (x == y) return y;
  for (int i = 17; i >= 0; i--)
    if (g[x][i] != g[y][i]) x = g[x][i], y = g[y][i];
  return g[x][0];
}
qry L[N + N];
int r;
int zc[N], sum[N];
int sumt[N], szt[N];
int ksm(int x, int y) {
  long long ret = 1;
  for (; y; y >>= 1) {
    if (y & 1) ret = ret * x % mo;
    x = (long long)x * x % mo;
  }
  return ret;
}
void change(int x, int v) {
  int sig = (v > 0) ? -1 : 1;
  for (; x <= n + 1; x += ((x) & -(x))) sumt[x] += v, szt[x] += sig;
}
void change1(int x, int v) {
  for (; x <= n + 1; x += ((x) & -(x))) szt[x] += v;
}
void change2(int x, int v) {
  for (; x <= n + 1; x += ((x) & -(x))) sumt[x] += v;
}
void query(int x, int &s, int &t, int xs) {
  for (; x; x -= ((x) & -(x))) s += sumt[x] * xs, t += szt[x] * xs;
}
void solve(int p) {
  r = 0;
  for (int i = qfinal[p]; i; i = qnex[i]) {
    L[++r] = qu[qto[i]];
    L[r].w = zs[i];
  }
  for (int i = ffinal[p]; i; i = fnex[i]) {
    L[++r].u = fto[i];
    L[r].w = fzs[i];
    L[r].no = 0;
    zc[L[r].u] = 1;
    change1(dfn[L[r].u], 1);
    change1(dfnr[L[r].u] + 1, -1);
  }
  sort(L + 1, L + 1 + r, cmp);
  for (int i = 1; i <= r; i++) {
    if (L[i].no == 0) {
      sum[L[i].u] += L[i].w;
      zc[L[i].u] = 0;
      change(dfn[L[i].u], L[i].w);
      change(dfnr[L[i].u] + 1, -L[i].w);
    } else {
      int cnt = 0, df = 0;
      query(dfn[L[i].u], df, cnt, 1);
      query(dfn[L[i].v], df, cnt, 1);
      query(dfn[L[i].lca], df, cnt, -2);
      df += (zc[L[i].lca] + cnt) * L[i].w;
      df += sum[L[i].lca];
      ans[L[i].no] = ((long long)ans[L[i].no] * ksm(p, df)) % mo;
    }
  }
  for (int i = 1; i <= r; i++) {
    if (L[i].no == 0) {
      change2(dfn[L[i].u], -L[i].w);
      change2(dfnr[L[i].u] + 1, +L[i].w);
      sum[L[i].u] = 0;
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    link(u, v), link(v, u);
  }
  dfs(1);
  init();
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    int z = a[i];
    while (z != 1) {
      int k = mf[z], cnt = 0;
      while (z % k == 0) cnt++, z /= k;
      fzs[++ftot] = cnt;
      fto[ftot] = i;
      fnex[ftot] = ffinal[k];
      ffinal[k] = ftot;
    }
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int z;
    scanf("%d %d %d\n", &qu[i].u, &qu[i].v, &z);
    qu[i].lca = getlca(qu[i].u, qu[i].v);
    qu[i].no = i;
    ans[i] = 1;
    while (z != 1) {
      int k = mf[z], cnt = 0;
      while (z % k == 0) cnt++, z /= k;
      zs[++qtot] = cnt;
      qto[qtot] = i;
      qnex[qtot] = qfinal[k];
      qfinal[k] = qtot;
    }
  }
  for (int i = 1; i <= p[0]; i++)
    if (ffinal[p[i]] && qfinal[p[i]] != 0) solve(p[i]);
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
}
