#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int N = 1e5 + 10;
const int M = 1e7 + 10;
const int A = 18;
struct node {
  int num;
  vector<pair<int, int> > item;
  friend bool operator<(const node &a, const node &b) { return a.num < b.num; }
} pri[N << 3];
struct query {
  int num, x, id;
  friend bool operator<(const query &a, const query &b) {
    return a.num < b.num;
  }
};
struct Ask {
  int x, y, z;
} ask[N];
vector<query> q[N << 3];
int et;
int k = 0, cnt = 0;
int n, m, ti;
int l[N], r[N], deep[N], c[N], f[N], have[M];
int p[N][A], ans[N * 32];
long long ret;
vector<int> v[N];
void dfs(int x, int fa, int dep) {
  l[x] = ++ti;
  deep[x] = dep;
  if (fa) {
    p[x][0] = fa;
    for (int i = 0; p[p[x][i]][i]; ++i) p[x][i + 1] = p[p[x][i]][i];
  }
  for (auto u : v[x]) {
    if (u == fa) continue;
    dfs(u, x, dep + 1);
  }
  r[x] = ti;
}
int LCA(int a, int b) {
  if (deep[a] < deep[b]) swap(a, b);
  for (int i = 0, delta = deep[a] - deep[b]; delta; delta >>= 1, ++i)
    if (delta & 1) a = p[a][i];
  if (a == b) return a;
  for (int i(17); i >= (0); --i)
    if (p[a][i] != p[b][i]) {
      a = p[a][i];
      b = p[b][i];
    }
  return p[a][0];
}
vector<pair<int, int> > split(int x) {
  vector<pair<int, int> > ret;
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      int cnt = 0;
      while (x % i == 0) x /= i, ++cnt;
      ret.push_back(make_pair(i, cnt));
    }
  }
  if (x > 1) ret.push_back(make_pair(x, 1));
  return ret;
}
inline void add(int x, int val) {
  for (; x <= n; x += x & -x) f[x] += val;
}
inline void update(int x, int val) {
  for (; x <= n; x += x & -x) c[x] += val;
}
inline int query_c(int x) {
  int ret = 0;
  for (; x; x -= x & -x) ret += c[x];
  return ret;
}
inline int query_f(int x) {
  int ret = 0;
  for (; x; x -= x & -x) ret += f[x];
  return ret;
}
inline long long Pow(long long a, long long b, long long mod) {
  long long ret(1);
  for (; b; b >>= 1, (a *= a) %= mod)
    if (b & 1) (ret *= a) %= mod;
  return ret;
}
inline void clr(int x) {
  for (; x <= n; x += x & -x) c[x] = f[x] = 0;
}
int main() {
  scanf("%d", &n);
  for (int i(2); i <= (n); ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  ti = 0;
  dfs(1, 0, 1);
  for (int i(1); i <= (n); ++i) {
    int num;
    scanf("%d", &num);
    vector<pair<int, int> > cc = split(num);
    for (auto u : cc) {
      int x = u.first, y = u.second;
      if (have[x]) {
        pri[have[x]].item.push_back(make_pair(y, i));
      } else {
        have[x] = ++cnt;
        pri[cnt].num = x;
        pri[cnt].item.push_back(make_pair(y, i));
      }
    }
  }
  for (int i(1); i <= (cnt); ++i) sort(pri[i].item.begin(), pri[i].item.end());
  sort(pri + 1, pri + cnt + 1);
  for (int i(1); i <= (cnt); ++i) have[pri[i].num] = i;
  scanf("%d", &m);
  for (int i(1); i <= (m); ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    ask[i] = {x, y, z};
    int lca = LCA(x, y), plca = p[lca][0];
    vector<pair<int, int> > cc = split(z);
    for (auto u : cc) {
      if (!have[u.first]) continue;
      int id = have[u.first];
      q[id].push_back({u.second, x, ++k});
      q[id].push_back({u.second, y, ++k});
      q[id].push_back({u.second, lca, ++k});
      q[id].push_back({u.second, plca, ++k});
    }
  }
  for (int i(1); i <= (cnt); ++i) sort(q[i].begin(), q[i].end());
  for (int i(1); i <= (cnt); ++i) {
    set<int> mark;
    for (auto u : pri[i].item) {
      int x = u.second, y = u.first;
      add(l[x], 1);
      add(r[x] + 1, -1);
      mark.insert(x);
    }
    int sz = pri[i].item.size();
    int now = 0;
    for (auto u : q[i]) {
      while (now < sz && u.num >= pri[i].item[now].first) {
        int x = pri[i].item[now].second;
        int y = pri[i].item[now].first;
        update(l[x], y);
        update(r[x] + 1, -y);
        add(l[x], -1);
        add(r[x] + 1, 1);
        mark.insert(x);
        ++now;
      }
      ans[u.id] = query_c(l[u.x]) + query_f(l[u.x]) * u.num;
    }
    for (auto u : mark) {
      clr(l[u]);
      clr(r[u] + 1);
    }
  }
  et = 0;
  for (int i(1); i <= (m); ++i) {
    int x = ask[i].x, y = ask[i].y, z = ask[i].z;
    ret = 1;
    int lca = LCA(x, y), plca = p[lca][0];
    vector<pair<int, int> > cc = split(z);
    for (auto u : cc) {
      if (!have[u.first]) continue;
      int now = 0;
      ++et;
      now += ans[et];
      ++et;
      now += ans[et];
      ++et;
      now -= ans[et];
      ++et;
      now -= ans[et];
      ret = ret * Pow(u.first, now, mod) % mod;
    }
    printf("%lld\n", ret);
  }
  return 0;
}
