#include <bits/stdc++.h>
#pragma GCC optimize("no-stack-protector")
using namespace std;
const int MAXBUF = 1000005;
char B[MAXBUF], *Si = B, *Ti = B;
inline char getc() {
  if (Si == Ti) Ti = (Si = B) + fread(B, 1, MAXBUF, stdin);
  if (Si == Ti) return 0;
  return *Si++;
}
template <class T>
inline void read(T &a) {
  static char c;
  while ((c = getc()) < '0' || c > '9')
    ;
  a = c - '0';
  while ((c = getc()) >= '0' && c <= '9') a = (a << 3) + (a << 1) + c - '0';
}
char Buff[MAXBUF], *sti = Buff;
template <class T>
inline void write(T a) {
  static char c[20];
  static int c0;
  c0 = 0;
  while (a) c[c0++] = a % 10 + '0', a /= 10;
  while (c0--) *sti++ = c[c0];
}
const int LM = 4000;
int head[100005], nxt[200005], v[200005], tot = 0;
int fa[100005], son[100005], sz[100005], top[100005];
int fla[10000005], pri[1000005], dep[100005], cnt = 0, n, m;
int c1[100005], c2[100005], res[100005];
int t1[4005][25], t2[10000005];
int ans[100005][11];
bool isp[10000005];
pair<int, int> p[100005][11];
pair<int, int> q[100005][11];
vector<int> inc[100005];
vector<int> del[100005];
inline int qpow(int a, int b) {
  int res = 1, tp = a;
  while (b) {
    if (b & 1) res = 1ll * res * tp % 1000000007;
    tp = 1ll * tp * tp % 1000000007;
    b >>= 1;
  }
  return res;
}
inline void add(int a, int b) {
  tot++;
  nxt[tot] = head[a];
  head[a] = tot;
  v[tot] = b;
}
inline void getp() {
  for (int i = 2; i <= 10000000; i++) {
    if (!isp[i]) {
      fla[i] = i;
      pri[++cnt] = i;
    }
    for (int j = 1; j <= cnt && i * pri[j] <= 10000000; j++) {
      fla[i * pri[j]] = pri[j];
      isp[i * pri[j]] = 1;
      if (i % pri[j] == 0) break;
    }
  }
}
void dfs(int pos, int f) {
  sz[pos] = 1;
  for (int i = head[pos]; i; i = nxt[i]) {
    if (v[i] == f) continue;
    dep[v[i]] = dep[pos] + 1;
    fa[v[i]] = pos;
    dfs(v[i], pos);
    sz[pos] += sz[v[i]];
    if (sz[v[i]] > sz[son[pos]]) son[pos] = v[i];
  }
}
void dfs2(int pos, int tp) {
  top[pos] = tp;
  if (son[pos]) dfs2(son[pos], tp);
  for (int i = head[pos]; i; i = nxt[i]) {
    if (v[i] == fa[pos]) continue;
    if (v[i] == son[pos]) continue;
    dfs2(v[i], v[i]);
  }
}
inline int lca(int a, int b) {
  while (top[a] ^ top[b]) {
    if (dep[top[a]] < dep[top[b]]) swap(a, b);
    a = fa[top[a]];
  }
  return (dep[a] < dep[b] ? a : b);
}
void work(int pos, int f) {
  int i, j, k;
  int s1 = inc[pos].size();
  int s2 = del[pos].size();
  for (i = 1; i <= c1[pos]; i++) {
    int num = p[pos][i].first;
    int o = p[pos][i].second;
    if (num <= LM) {
      for (j = 1; j <= o; j++) t1[num][j]++;
      continue;
    }
    t2[num]++;
  }
  for (i = 0; i < s1; i++) {
    int id = inc[pos][i];
    for (j = 1; j <= c2[id]; j++) {
      int num = q[id][j].first;
      int o = q[id][j].second;
      if (num <= LM) {
        for (k = 1; k <= o; k++) ans[id][j] += t1[num][k];
        continue;
      }
      ans[id][j] += t2[num];
    }
  }
  for (i = 0; i < s2; i++) {
    int id = del[pos][i];
    for (j = 1; j <= c2[id]; j++) {
      int num = q[id][j].first;
      int o = q[id][j].second;
      if (num <= LM) {
        for (k = 1; k <= o; k++) ans[id][j] -= t1[num][k];
        continue;
      }
      ans[id][j] -= t2[num];
    }
  }
  for (i = head[pos]; i; i = nxt[i]) {
    if (v[i] == f) continue;
    work(v[i], pos);
  }
  for (i = 1; i <= c1[pos]; i++) {
    int num = p[pos][i].first;
    int o = p[pos][i].second;
    if (num <= LM) {
      for (j = 1; j <= o; j++) t1[num][j]--;
      continue;
    }
    t2[num]--;
  }
}
int main() {
  int a, b, x, i, j;
  getp();
  read(n);
  for (i = 1; i < n; i++) {
    read(a);
    read(b);
    add(a, b);
    add(b, a);
  }
  dfs(1, 0);
  dfs2(1, 1);
  for (i = 1; i <= n; i++) {
    read(x);
    while (x != 1) {
      int t = fla[x], o = 0;
      while (x % t == 0) x /= t, o++;
      p[i][++c1[i]] = make_pair(t, o);
    }
  }
  read(m);
  for (i = 1; i <= m; i++) {
    read(a);
    read(b);
    read(x);
    int t = lca(a, b);
    while (x != 1) {
      int t = fla[x], o = 0;
      while (x % t == 0) x /= t, o++;
      q[i][++c2[i]] = make_pair(t, o);
    }
    inc[a].push_back(i);
    inc[b].push_back(i);
    del[t].push_back(i);
    del[fa[t]].push_back(i);
  }
  work(1, 0);
  for (i = 1; i <= m; i++) {
    int res = 1;
    for (j = 1; j <= c2[i]; j++)
      res = 1ll * res * qpow(q[i][j].first, ans[i][j]) % 1000000007;
    write(res), *sti++ = '\n';
  }
  fwrite(Buff, 1, sti - Buff, stdout);
  return 0;
}
