#include <bits/stdc++.h>
using namespace std;
namespace cqz {
static const int GYN = 2333333;
char SZB[GYN], *S = SZB, *T = SZB;
inline char gc() {
  if (S == T) {
    T = (S = SZB) + fread(SZB, 1, GYN, stdin);
    if (S == T) return '\n';
  }
  return *S++;
}
inline long long read() {
  long long x = 0, g = 1;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    if (ch == '-') g = -1;
  for (; isdigit(ch); ch = gc()) x = x * 10 - 48 + ch;
  return x * g;
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
inline char readchar() {
  char ch = gc();
  for (; isspace(ch); ch = gc())
    ;
  return ch;
}
inline long long readstr(char *s) {
  char ch = gc();
  int cur = 0;
  for (; isspace(ch); ch = gc())
    ;
  for (; !isspace(ch); ch = gc()) s[cur++] = ch;
  s[cur] = '\0';
  return cur;
}
void Print(long long *a, int s, int t) {
  for (int i = int(s); i <= int(t); ++i) printf("%lld ", a[i]);
}
void Print(int *a, int s, int t) {
  for (int i = int(s); i <= int(t); ++i) printf("%d ", a[i]);
}
void Print(char *a, int s, int t) {
  for (int i = int(s); i <= int(t); ++i) putchar(a[i]);
}
void writeln(long long x) {
  write(x);
  puts("");
}
}  // namespace cqz
using namespace cqz;
const long long N = 1000010, mod = 1e9 + 7;
long long Q, ind, n, fa[200010][22], L[N], R[N], c[N], pri[N], dep[N], W[N],
    ans[N], x[N], y[N], w[N];
long long mark[10000010];
vector<long long> g[N];
vector<pair<long long, long long> > has[N];
void Init() {
  long long n = 10000000;
  for (int i = int(2); i <= int(n); ++i)
    if (!mark[i]) {
      pri[++pri[0]] = i;
      for (long long j = i; j <= n; j += i)
        mark[j] = mark[j] ? mark[j] : pri[0];
    }
}
void add(long long x, long long v) {
  for (; x <= n; x += x & -x) c[x] += v;
}
long long ask(long long x) {
  long long ans = 0;
  for (; x; x -= x & -x) ans += c[x];
  return ans;
}
void dfs(long long x) {
  L[x] = ++ind;
  for (int i = int(1); i <= int(20); ++i) fa[x][i] = fa[fa[x][i - 1]][i - 1];
  for (int i = int(0); i < int(g[x].size()); ++i)
    if (g[x][i] != fa[x][0]) {
      long long to = g[x][i];
      dep[to] = dep[x] + 1;
      fa[to][0] = x;
      dfs(to);
    }
  R[x] = ind;
}
long long lca(long long a, long long b) {
  if (dep[a] < dep[b]) swap(a, b);
  for (int i = int(20); i >= int(0); --i)
    if (dep[a] - (1 << i) >= dep[b]) a = fa[a][i];
  for (int i = int(20); i >= int(0); --i)
    if (fa[a][i] != fa[b][i]) a = fa[a][i], b = fa[b][i];
  return a == b ? a : fa[a][0];
}
void work(long long x, long long w) {
  for (; w > 1;) {
    long long cnt = 0, cur = mark[w];
    for (; !(w % pri[cur]); w /= pri[cur], ++cnt)
      if (x >= 0) has[cur].push_back(make_pair(cnt + 1, x));
    if (x < 0) has[cur].push_back(make_pair(cnt, x));
  }
}
long long ppow(long long x, long long k) {
  long long ans = 1;
  for (; k; k >>= 1, ((x) = 1LL * (x) * (x) % mod))
    if (k & 1) ((ans) = 1LL * (ans) * (x) % mod);
  return ans;
}
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
  return a.first == b.first ? (a.second > b.second) : (a.first < b.first);
}
int main() {
  n = read();
  Init();
  for (int i = int(1); i < int(n); ++i) {
    long long x = read(), y = read();
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = int(1); i <= int(n); ++i) W[i] = read();
  dfs(1);
  Q = read();
  for (int i = int(1); i <= int(n); ++i) work(i, W[i]);
  for (int i = int(1); i <= int(Q); ++i) {
    x[i] = read(), y[i] = read(), w[i] = read(), ans[i] = 1;
    work(-i, w[i]);
  }
  for (int i = int(1); i <= int(pri[0]); ++i)
    if (has[i].size()) {
      sort(has[i].begin(), has[i].end(), cmp);
      for (int j = int(0); j < int(has[i].size()); ++j) {
        if (has[i][j].second >= 0) {
          add(L[has[i][j].second], 1);
          add(R[has[i][j].second] + 1, -1);
        } else {
          long long a = x[-has[i][j].second], b = y[-has[i][j].second],
                    Lca = lca(a, b),
                    Value = ask(L[a]) + ask(L[b]) - ask(L[Lca]) -
                            ask(L[fa[Lca][0]]);
          ((ans[-has[i][j].second]) =
               1LL * (ans[-has[i][j].second]) * (ppow(pri[i], Value)) % mod);
        }
      }
      for (int j = int(0); j < int(has[i].size()); ++j)
        if (has[i][j].second >= 0) {
          add(L[has[i][j].second], -1);
          add(R[has[i][j].second] + 1, 1);
        }
    }
  for (int i = int(1); i <= int(Q); ++i) writeln(ans[i]);
}
