#include <bits/stdc++.h>
#pragma warning(disable : 4996)
template <typename T>
T min(T x, T y) {
  return x < y ? x : y;
}
template <typename T>
T max(T x, T y) {
  return x > y ? x : y;
};
const int MAXN = 100005;
const long long mod = 1000000007;
long long qpow(long long x, long long y) {
  if (!y) return 1;
  long long t = qpow(x, y / 2);
  t = t * t % mod;
  if (y & 1) t = t * x % mod;
  return t;
}
std::pair<int, int> V[100];
int VS;
void sperate(int x) {
  int t = x;
  VS = 0;
  for (int i = 2; i * i <= x; i++) {
    if (t % i) continue;
    int cnt = 0;
    while (t % i == 0) cnt++, t /= i;
    V[++VS] = std::make_pair(i, cnt);
  }
  if (t > 1) V[++VS] = std::make_pair(t, 1);
}
struct node {
  int to;
  node *next;
};
void addnode(node *&head, int to) {
  node *p = new node;
  p->to = to;
  p->next = head;
  head = p;
}
struct opt {
  int ty;
  int val;
  int num;
  int pos;
  int tag;
  opt(int t = 0, int v = 0, int n = 0, int p = 0, int ta = 0) {
    ty = t;
    val = v;
    num = n;
    pos = p;
    tag = ta;
  }
  bool operator<(opt t) const {
    if (val != t.val) return val < t.val;
    if (num != t.num) return num < t.num;
    return ty < t.ty;
  }
};
int N, M;
long long ans[MAXN];
node *edge[MAXN];
int rank[MAXN], mxr[MAXN], fa[MAXN][25], dep[MAXN];
opt op[MAXN * 50];
int cnt;
int dfs_clock;
void dfs(int v, int f) {
  dep[v] = dep[f] + 1;
  fa[v][0] = f;
  for (int i = 1; i < 25; i++) fa[v][i] = fa[fa[v][i - 1]][i - 1];
  rank[v] = ++dfs_clock;
  for (node *p = edge[v]; p; p = p->next)
    if (p->to != f) dfs(p->to, v);
  mxr[v] = dfs_clock;
}
int LCA(int x, int y) {
  if (dep[y] > dep[x]) std::swap(x, y);
  int d = dep[x] - dep[y];
  for (int i = 0; i < 25; i++)
    if (d & (1 << i)) x = fa[x][i];
  if (x == y) return x;
  for (int i = 24; i >= 0; i--)
    if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}
void init() {
  int u, v, w;
  scanf("%d", &N);
  for (int i = 1; i < N; i++) {
    scanf("%d %d", &u, &v);
    addnode(edge[u], v);
    addnode(edge[v], u);
  }
  dfs(1, 0);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &u);
    sperate(u);
    for (std::pair<int, int> *it = V + 1; it != V + VS + 1; it++)
      op[++cnt] = opt(0, (*it).first, (*it).second, i);
  }
  scanf("%d", &M);
  for (int i = 1; i <= M; i++) {
    ans[i] = 1;
    scanf("%d %d %d", &u, &v, &w);
    sperate(w);
    for (std::pair<int, int> *it = V + 1; it != V + VS + 1; it++)
      op[++cnt] = opt(i, (*it).first, (*it).second, u, v);
  }
  std::sort(op + 1, op + cnt + 1);
}
struct tree {
  long long bit[MAXN];
  void modify(int l, int r, int k) {
    for (int i = l; i < MAXN; i += ((i) & -(i))) bit[i] += k;
    for (int i = r + 1; i < MAXN; i += ((i) & -(i))) bit[i] -= k;
  }
  long long query(int id) {
    long long ret = 0;
    for (int i = id; i; i -= ((i) & -(i))) ret += bit[i];
    return ret;
  }
};
tree T1, T2;
void solve() {
  for (int i = 1; i <= cnt && op[i].val == op[1].val; i++)
    if (!op[i].ty) T2.modify(rank[op[i].pos], mxr[op[i].pos], 1);
  for (int i = 1; i <= cnt; i++) {
    if (i > 1 && op[i].val != op[i - 1].val) {
      for (int j = i - 1; j && op[j].val == op[i - 1].val; j--)
        if (!op[j].ty) T1.modify(rank[op[j].pos], mxr[op[j].pos], -op[j].num);
      for (int j = i; j <= cnt && op[j].val == op[i].val; j++)
        if (!op[j].ty) T2.modify(rank[op[j].pos], mxr[op[j].pos], 1);
    }
    if (!op[i].ty) {
      T1.modify(rank[op[i].pos], mxr[op[i].pos], op[i].num);
      T2.modify(rank[op[i].pos], mxr[op[i].pos], -1);
    } else {
      long long t = 0;
      t += T1.query(rank[op[i].pos]);
      t += T2.query(rank[op[i].pos]) * op[i].num;
      t += T1.query(rank[op[i].tag]);
      t += T2.query(rank[op[i].tag]) * op[i].num;
      int lca = LCA(op[i].pos, op[i].tag);
      t -= T1.query(rank[lca]);
      t -= T2.query(rank[lca]) * op[i].num;
      t -= T1.query(rank[fa[lca][0]]);
      t -= T2.query(rank[fa[lca][0]]) * op[i].num;
      ans[op[i].ty] = ans[op[i].ty] * qpow(op[i].val, t) % mod;
    }
  }
}
int main() {
  init();
  solve();
  for (int i = 1; i <= M; i++) printf("%I64d\n", ans[i]);
  return 0;
}
