#include <bits/stdc++.h>
using namespace std;
const int lim = 10000001, mod = 1e9 + 7;
char vaz[lim + 10];
vector<int> primes, v[100010], normm, cnt[2000010];
vector<pair<int, int>> divz[100010], divzq[100010], qry[100010];
int l, first[100010], niv[100010], logg[200010], rmq[200010][20], ans[100010],
    tat[100010];
void ciur() {
  for (int i = 2; i <= lim; i++)
    if (vaz[i] == 0) {
      primes.push_back(i);
      for (long long j = 1LL * i * i; j <= lim; j += i) vaz[j] = 1;
    }
}
int rid_put(int a, int b) {
  int sol = 1;
  for (int i = 1; i <= b; i <<= 1) {
    if (b & i) sol = (1LL * sol * a) % mod;
    a = (1LL * a * a) % mod;
  }
  return sol;
}
void dfs1(int nod, int nivel, int tata) {
  niv[nod] = nivel;
  rmq[++l][0] = nod;
  first[nod] = l;
  tat[nod] = tata;
  for (int i = 0; i < v[nod].size(); i++)
    if (v[nod][i] != tata) {
      dfs1(v[nod][i], nivel + 1, nod);
      rmq[++l][0] = nod;
    }
}
int lca(int a, int b) {
  a = first[a];
  b = first[b];
  if (a > b) swap(a, b);
  int l1 = logg[b - a + 1];
  if (niv[rmq[a][l1]] < niv[rmq[b - (1 << l1) + 1][l1]])
    return rmq[a][l1];
  else
    return rmq[b - (1 << l1) + 1][l1];
}
void dfs2(int nod, int tata) {
  for (int i = 0; i < divz[nod].size(); i++) {
    int poz = lower_bound(normm.begin(), normm.end(), divz[nod][i].first) -
              normm.begin();
    int nr = divz[nod][i].second;
    if (nr + 1 > cnt[poz].size()) cnt[poz].resize(nr + 1);
    cnt[poz][nr]++;
  }
  for (int i = 0; i < qry[nod].size(); i++) {
    int q = qry[nod][i].first;
    for (int j = 0; j < divzq[q].size(); j++) {
      int poz = lower_bound(normm.begin(), normm.end(), divzq[q][j].first) -
                normm.begin();
      int nr = 0;
      for (int k = 1; k < cnt[poz].size(); k++)
        if (divzq[q][j].second >= k)
          nr += k * cnt[poz][k];
        else
          nr += divzq[q][j].second * cnt[poz][k];
      nr = rid_put(divzq[q][j].first, nr);
      if (qry[nod][i].second == -1) nr = rid_put(nr, mod - 2);
      ans[q] = (1LL * ans[q] * nr) % mod;
    }
  }
  for (int i = 0; i < v[nod].size(); i++)
    if (v[nod][i] != tata) dfs2(v[nod][i], nod);
  for (int i = 0; i < divz[nod].size(); i++) {
    int poz = lower_bound(normm.begin(), normm.end(), divz[nod][i].first) -
              normm.begin();
    cnt[poz][divz[nod][i].second]--;
  }
}
int main() {
  int n, x, y, c, q;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  ciur();
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    for (int j = 0; j < primes.size(); j++) {
      if (1LL * primes[j] * primes[j] > x) break;
      if (x % primes[j] != 0) continue;
      int nr = 0;
      while (x % primes[j] == 0) {
        x /= primes[j];
        nr++;
      }
      divz[i].push_back({primes[j], nr});
      normm.push_back(primes[j]);
    }
    if (x > 1) {
      divz[i].push_back({x, 1});
      normm.push_back(x);
    }
  }
  dfs1(1, 0, 0);
  logg[1] = 0;
  for (int i = 2; i <= l; i++) logg[i] = logg[i / 2] + 1;
  for (int i = 1; i <= logg[l]; i++)
    for (int j = 1; j + (1 << i) - 1 <= l; j++)
      if (niv[rmq[j][i - 1]] < niv[rmq[j + (1 << (i - 1))][i - 1]])
        rmq[j][i] = rmq[j][i - 1];
      else
        rmq[j][i] = rmq[j + (1 << (i - 1))][i - 1];
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d%d", &x, &y, &c);
    ans[i] = 1;
    int z = lca(x, y);
    qry[x].push_back({i, 1});
    qry[y].push_back({i, 1});
    qry[z].push_back({i, -1});
    qry[tat[z]].push_back({i, -1});
    for (int j = 0; j < primes.size(); j++) {
      if (1LL * primes[j] * primes[j] > c) break;
      if (c % primes[j] != 0) continue;
      int nr = 0;
      while (c % primes[j] == 0) {
        c /= primes[j];
        nr++;
      }
      divzq[i].push_back({primes[j], nr});
      normm.push_back(primes[j]);
    }
    if (c > 1) {
      divzq[i].push_back({c, 1});
      normm.push_back(c);
    }
  }
  sort(normm.begin(), normm.end());
  normm.erase(unique(normm.begin(), normm.end()), normm.end());
  dfs2(1, 0);
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}
