#include <bits/stdc++.h>
const int con = 1000000007;
const int maxv = 10000000;
const int maxd = 18;
int n, m;
int prime[10000100];
long long pre[10000100];
int inv[10000100];
int lca[101000][20], dep[101000];
long long ans[100100];
int a[101000];
int adj[101000], subs[303000], aim[303000];
int qadj[101000], qsubs[403000], qaim[403000], cost[403000], q[101000];
int tot;
long long power(long long x, int y) {
  long long re;
  re = 1;
  while (y) {
    if (y & 1) re = re * x % con;
    x = x * x % con;
    y /= 2;
  }
  return re;
}
void builddfs(int x, int p) {
  lca[x][0] = p;
  dep[x] = dep[p] + 1;
  int i = adj[x];
  while (i > 0) {
    if (aim[i] != p) builddfs(aim[i], x);
    i = subs[i];
  }
}
void dfs(int x) {
  int temp, cumu;
  temp = a[x];
  cumu = 1;
  while (temp > 1) {
    if (cumu % prime[temp] > 0)
      cumu = prime[temp];
    else
      cumu *= prime[temp];
    pre[cumu] = pre[cumu] * prime[temp] % con;
    temp /= prime[temp];
  }
  int i;
  long long sum;
  i = qadj[x];
  while (i > 0) {
    temp = q[qaim[i]];
    cumu = 1;
    sum = 1;
    while (temp > 1) {
      if (cumu % prime[temp] > 0)
        cumu = prime[temp];
      else
        cumu *= prime[temp];
      sum = sum * pre[cumu] % con;
      temp /= prime[temp];
    }
    ans[qaim[i]] = ans[qaim[i]] * power(sum, cost[i]) % con;
    i = qsubs[i];
  }
  i = adj[x];
  while (i > 0) {
    if (aim[i] != lca[x][0]) dfs(aim[i]);
    i = subs[i];
  }
  temp = a[x];
  cumu = 1;
  while (temp > 1) {
    if (cumu % prime[temp] > 0)
      cumu = prime[temp];
    else
      cumu *= prime[temp];
    if (inv[prime[temp]] == 0) inv[prime[temp]] = power(prime[temp], con - 2);
    pre[cumu] = pre[cumu] * inv[prime[temp]] % con;
    temp /= prime[temp];
  }
}
int main() {
  pre[1] = 1;
  for (int i = 2; i <= maxv; ++i) {
    pre[i] = 1;
    if (prime[i] == 0) {
      for (int j = 1; j <= maxv; ++j) {
        if (i * j > maxv) break;
        prime[i * j] = i;
      }
    }
  }
  scanf("%d", &n);
  int u, v;
  for (int i = 1; i < n; ++i) {
    scanf("%d %d", &u, &v);
    aim[i * 2] = v;
    subs[i * 2] = adj[u];
    adj[u] = i * 2;
    aim[i * 2 + 1] = u;
    subs[i * 2 + 1] = adj[v];
    adj[v] = i * 2 + 1;
  }
  builddfs(1, 1);
  for (int j = 1; j <= maxd; ++j)
    for (int i = 1; i <= n; ++i) lca[i][j] = lca[lca[i][j - 1]][j - 1];
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  scanf("%d", &m);
  int temp;
  for (int i = 1; i <= m; ++i) {
    ans[i] = 1;
    scanf("%d %d %d", &u, &v, &q[i]);
    ++tot;
    qaim[tot] = i;
    qsubs[tot] = qadj[u];
    qadj[u] = tot;
    cost[tot] = 1;
    ++tot;
    qaim[tot] = i;
    qsubs[tot] = qadj[v];
    qadj[v] = tot;
    cost[tot] = 1;
    if (dep[u] < dep[v]) {
      temp = u;
      u = v;
      v = temp;
    }
    for (int j = maxd; j >= 0; --j)
      if (dep[lca[u][j]] >= dep[v]) u = lca[u][j];
    for (int j = maxd; j >= 0; --j) {
      if (lca[u][j] != lca[v][j]) {
        u = lca[u][j];
        v = lca[v][j];
      }
    }
    if (u != v) {
      u = lca[u][0];
    }
    ++tot;
    qaim[tot] = i;
    qsubs[tot] = qadj[u];
    qadj[u] = tot;
    cost[tot] = con - 2;
    if (u != 1) {
      u = lca[u][0];
      ++tot;
      qaim[tot] = i;
      qsubs[tot] = qadj[u];
      qadj[u] = tot;
      cost[tot] = con - 2;
    }
  }
  dfs(1);
  for (int i = 1; i <= m; ++i) printf("%I64d\n", ans[i]);
  return 0;
}
