#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int P = 1e7 + 5;
const int MOD = 1e9 + 7;
int n, q, pn;
int isp[P];
int a[N], c[N];
int dep[N], fa[25][N];
int ans[N];
int ct[N * 10][30];
int mt[N * 10][30];
vector<int> nei[N];
vector<pair<int, int> > qry[N];
int Pow(int x, int y) {
  int rt = 1, mt = x;
  while (y) {
    if (y & 1) rt = 1ll * rt * mt % MOD;
    y >>= 1;
    mt = 1ll * mt * mt % MOD;
  }
  return rt;
}
void dfs(int x) {
  for (int i = 0; i < int(nei[x].size()); i++) {
    int to = nei[x][i];
    if (to == fa[0][x]) continue;
    fa[0][to] = x;
    dep[to] = dep[x] + 1;
    dfs(to);
  }
}
void cal(int x) {
  int y = a[x];
  for (int i = 2; i * i <= y; i++)
    if (y % i == 0) {
      int pw = 0;
      while (y % i == 0) {
        pw++;
        y /= i;
      }
      ct[isp[i]][pw]++;
      mt[isp[i]][pw] = 1ll * mt[isp[i]][pw] * Pow(i, pw) % MOD;
    }
  if (y != 1) {
    ct[isp[y]][1]++;
    mt[isp[y]][1] = 1ll * mt[isp[y]][1] * y % MOD;
  }
  for (int i = 0; i < int(qry[x].size()); i++) {
    int qr = qry[x][i].first;
    y = c[qr];
    for (int j = 2; j * j <= y; j++)
      if (y % j == 0) {
        int pw = 0;
        while (y % j == 0) {
          pw++;
          y /= j;
        }
        int tot = 0, mul = 1;
        for (int k = 1; k <= pw; k++)
          mul = 1ll * mul * Pow(j, k * ct[isp[j]][k]) % MOD;
        for (int k = pw + 1; k <= 25; k++) tot += ct[isp[j]][k];
        mul = 1ll * mul * Pow(j, pw * tot) % MOD;
        if (qry[x][i].second == 1)
          ans[qr] = 1ll * ans[qr] * mul % MOD;
        else
          ans[qr] = 1ll * ans[qr] * Pow(mul, MOD - 2) % MOD;
      }
    if (y != 1) {
      int pw = 1;
      int tot = 0, mul = 1;
      for (int k = 1; k <= pw; k++) mul = 1ll * mul * mt[isp[y]][k] % MOD;
      for (int k = pw + 1; k <= 25; k++) tot += ct[isp[y]][k];
      mul = 1ll * mul * Pow(y, pw * tot) % MOD;
      if (qry[x][i].second == 1)
        ans[qr] = 1ll * ans[qr] * mul % MOD;
      else
        ans[qr] = 1ll * ans[qr] * Pow(mul, MOD - 2) % MOD;
    }
  }
  for (int i = 0; i < int(nei[x].size()); i++) {
    int to = nei[x][i];
    if (to == fa[0][x]) continue;
    cal(to);
  }
  y = a[x];
  for (int i = 2; i * i <= y; i++)
    if (y % i == 0) {
      int pw = 0;
      while (y % i == 0) {
        pw++;
        y /= i;
      }
      ct[isp[i]][pw]--;
      mt[isp[i]][pw] =
          1ll * mt[isp[i]][pw] * Pow(i, 1ll * pw * (MOD - 2) % MOD) % MOD;
    }
  if (y != 1) {
    ct[isp[y]][1]--;
    mt[isp[y]][1] = 1ll * mt[isp[y]][1] * Pow(y, MOD - 2) % MOD;
  }
}
int lca(int x, int y) {
  if (dep[x] > dep[y]) swap(x, y);
  for (int i = 0; i < 24; i++)
    if ((dep[y] - dep[x]) & (1 << i)) y = fa[i][y];
  if (x == y) return x;
  for (int i = 23; i >= 0; i--)
    if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
  return fa[0][x];
}
int main() {
  for (int i = 0; i < N * 10; i++)
    for (int j = 0; j < 30; j++) mt[i][j] = 1;
  for (int i = 2; i < P; i++) isp[i] = 1;
  for (int i = 2; i < P; i++)
    if (isp[i]) {
      isp[i] = ++pn;
      for (int j = 2; i * j < P; j++) isp[i * j] = 0;
    }
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    nei[x].push_back(y);
    nei[y].push_back(x);
  }
  fa[0][1] = 1;
  dfs(1);
  for (int i = 0; i < 24; i++)
    for (int j = 1; j <= n; j++) fa[i + 1][j] = fa[i][fa[i][j]];
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int x, y, l;
    scanf("%d%d%d", &x, &y, c + i);
    l = lca(x, y);
    if (l != 1) qry[fa[0][l]].push_back(make_pair(i, -1));
    qry[l].push_back(make_pair(i, -1));
    qry[x].push_back(make_pair(i, 1));
    qry[y].push_back(make_pair(i, 1));
    ans[i] = 1;
  }
  cal(1);
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}
