#include <bits/stdc++.h>
using namespace std;
const int Maxn = 100010;
const int lg = 18;
const long long Mod = 1e9 + 7;
int minp[10000010], pr[1000010], prl, po[10000010];
void pre() {
  minp[1] = 1;
  for (int i = 2; i <= 10000000; i++) {
    if (!minp[i]) minp[i] = i, pr[++prl] = i, po[i] = prl;
    for (int j = 1; j <= prl && i * pr[j] <= 10000000; j++) {
      minp[i * pr[j]] = pr[j];
      if (i % pr[j] == 0) break;
    }
  }
}
struct node {
  int y, next;
} a[Maxn << 1];
int first[Maxn], len;
void ins(int x, int y) {
  len++;
  a[len].y = y;
  a[len].next = first[x];
  first[x] = len;
}
int n, m;
int na[Maxn];
int fa[Maxn][lg], dep[Maxn];
long long ans[Maxn];
int qq[Maxn];
vector<int> vec[Maxn], fl[Maxn];
int ss[1000010][25];
void dfs(int x) {
  for (int i = 1; i < lg; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
  for (int k = first[x]; k; k = a[k].next) {
    int y = a[k].y;
    if (y == fa[x][0]) continue;
    fa[y][0] = x;
    dep[y] = dep[x] + 1;
    dfs(y);
  }
}
int getlca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = lg - 1; i >= 0; i--) {
    if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
  }
  if (x == y) return x;
  for (int i = lg - 1; i >= 0; i--) {
    if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  }
  return fa[x][0];
}
int pi[lg], ri[lg], K;
void fj(int x) {
  K = 0;
  while (x > 1) {
    if (minp[x] == pi[K])
      ri[K]++;
    else
      pi[++K] = minp[x], ri[K] = 1;
    x /= minp[x];
  }
}
long long qpow(long long x, int k) {
  long long ret = 1;
  while (k) {
    if (k & 1) ret = ret * x % Mod;
    x = x * x % Mod;
    k >>= 1;
  }
  return ret;
}
void dfs2(int x) {
  fj(na[x]);
  int i, j, k;
  for (i = 1; i <= K; i++) ss[po[pi[i]]][ri[i]]++;
  for (i = 0; i < vec[x].size(); i++) {
    int num = vec[x][i];
    fj(qq[num]);
    for (j = 1; j <= K; j++) {
      int ps = 0;
      for (k = 1; k < 25; k++) {
        if (k <= ri[j])
          ps += ss[po[pi[j]]][k] * k;
        else
          ps += ss[po[pi[j]]][k] * ri[j];
      }
      if (fl[x][i] == 1)
        ans[num] = ans[num] * qpow(pi[j], ps) % Mod;
      else
        ans[num] = ans[num] * qpow(qpow(pi[j], ps), Mod - 2) % Mod;
    }
  }
  for (k = first[x]; k; k = a[k].next) {
    int y = a[k].y;
    if (y == fa[x][0]) continue;
    dfs2(y);
  }
  fj(na[x]);
  for (i = 1; i <= K; i++) ss[po[pi[i]]][ri[i]]--;
}
int main() {
  int i, j, k;
  pre();
  scanf("%d", &n);
  for (i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ins(x, y);
    ins(y, x);
  }
  dep[1] = 1;
  dfs(1);
  for (i = 1; i <= n; i++) scanf("%d", &na[i]);
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    int x, y;
    ans[i] = 1;
    scanf("%d%d%d", &x, &y, &qq[i]);
    int lca = getlca(x, y);
    vec[x].push_back(i);
    fl[x].push_back(1);
    vec[y].push_back(i);
    fl[y].push_back(1);
    vec[lca].push_back(i);
    fl[lca].push_back(-1);
    vec[fa[lca][0]].push_back(i);
    fl[fa[lca][0]].push_back(-1);
  }
  dfs2(1);
  for (i = 1; i <= m; i++) printf("%I64d\n", ans[i]);
  return 0;
}
