Problem: Let the main characters of this problem be personages from some recent movie. New Avengers seem to make a lot of buzz. I didn't watch any part of the franchise and don't know its heroes well, but it won't stop me from using them in this problem statement. So, Thanos and Dr. Strange are doing their superhero and supervillain stuff, but then suddenly they stumble across a regular competitive programming problem.

You are given a tree with n vertices.

In each vertex v there is positive integer a_{v}.

You have to answer q queries.

Each query has a from u v x.

You have to calculate ∏_{w ∈ P} gcd(x, a_{w}) mod (10^{9} + 7), where P is a set of vertices on path from u to v. In other words, you are to calculate the product of gcd(x, a_{w}) for all vertices w on the path from u to v. As it might be large, compute it modulo 10^9+7. Here gcd(s, t) denotes the greatest common divisor of s and t.

Note that the numbers in vertices do not change after queries.

I suppose that you are more interested in superhero business of Thanos and Dr. Strange than in them solving the problem. So you are invited to solve this problem instead of them.

Input

In the first line of input there is one integer n (1 ≤ n ≤ 10^{5}) — the size of the tree.

In the next n-1 lines the edges of the tree are described. The i-th edge is described with two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n) and it connects the vertices u_{i} and v_{i}. It is guaranteed that graph with these edges is a tree.

In the next line there are n integers a_1, a_2, …, a_n (1 ≤ a_{v} ≤ 10^{7}).

In the next line there is one integer q (1 ≤ q ≤ 10^{5}) — the number of queries.

And in the next q lines the queries are described. Each query is described with three integers u_{i}, v_{i} and x_{i} (1 ≤ u_{i}, v_{i} ≤ n, 1 ≤ x_{i} ≤ 10^{7}).

Output

Print q numbers — the answers to the queries in the order they are given in the input. Print each answer modulo 10^9+7 = 1000000007. Print each number on a separate line.

Examples

Input

4
1 2
1 3
1 4
6 4 9 5
3
2 3 6
2 3 2
3 4 7


Output

36
4
1


Input

6
1 2
2 3
2 4
1 5
5 6
100000 200000 500000 40000 800000 250000
3
3 5 10000000
6 2 3500000
4 1 64000


Output

196000
12250
999998215

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
const int M = 1000000007;
int n, v[N];
vector<vector<int> > g;
vector<vector<pair<int, int> > > f;
bool notPrime[10001] = {1, 1};
int p[N];
int d[N];
int sz[N];
void DFS(int u) {
  sz[u] = 1;
  for (auto &v : g[u]) {
    if (v != p[u]) {
      p[v] = u;
      d[v] = d[u] + 1;
      DFS(v);
      sz[u] += sz[v];
    }
  }
}
int c, ch[N], chl[N], chr[N], chH[N];
int eb[N], ee[N];
vector<pair<int, int> > val;
void build(int u) {
  if (ch[u] == -1) {
    ch[u] = c;
    chH[c] = u;
    chl[c] = val.size();
    chr[c] = chl[c] - 1;
    ++c;
  }
  eb[u] = val.size();
  for (auto &x : f[u]) {
    val.push_back(x);
    ++chr[ch[u]];
  }
  ee[u] = val.size() - 1;
  int mx = -1, at = -1;
  for (auto &v : g[u])
    if (v != p[u]) {
      if (mx < sz[v]) {
        mx = sz[v];
        at = v;
      }
    }
  if (mx != -1) {
    ch[at] = ch[u];
    build(at);
  }
  for (auto &v : g[u])
    if (v != at && v != p[u]) build(v);
}
vector<pair<int, int> > seg[8 * 4 * N];
vector<int> cs[8 * 4 * N];
void build(int n, int s, int e) {
  if (s == e) {
    seg[n].push_back(val[s]);
    cs[n].push_back(val[s].second);
    return;
  }
  build(n * 2, s, (s + e) / 2);
  build(n * 2 + 1, (s + e) / 2 + 1, e);
  auto &a = seg[n * 2];
  auto &b = seg[n * 2 + 1];
  auto &c = seg[n];
  int i = 0, j = 0;
  while (i < a.size() && j < b.size()) {
    if (a[i] <= b[j])
      c.push_back(a[i++]);
    else
      c.push_back(b[j++]);
  }
  while (i < a.size()) c.push_back(a[i++]);
  while (j < b.size()) c.push_back(b[j++]);
  int total = 0;
  for (int i = 0; i < seg[n].size(); ++i) {
    total += seg[n][i].second;
    cs[n].push_back(total);
  }
}
vector<pair<int, int> > cur;
vector<pair<int, int> > ans;
int ql, qr;
int w;
void get(int n, int s, int e) {
  if (qr < s || ql > e) return;
  if (s >= ql && e <= qr) {
    for (int i = 0; i < cur.size(); ++i) {
      auto &a = cur[i];
      int l = lower_bound(seg[n].begin(), seg[n].end(), make_pair(a.first, 0)) -
              seg[n].begin();
      int m = lower_bound(seg[n].begin(), seg[n].end(),
                          make_pair(a.first, a.second + 1)) -
              seg[n].begin();
      --m;
      int r =
          lower_bound(seg[n].begin(), seg[n].end(), make_pair(a.first + 1, 0)) -
          seg[n].begin();
      --r;
      if (l <= r) {
        if (m >= 0) ans[i].second += cs[n][m];
        if (l > 0) ans[i].second -= cs[n][l - 1];
        ans[i].second += a.second * (r - m);
      }
    }
    return;
  }
  get(n * 2, s, (s + e) / 2);
  get(n * 2 + 1, (s + e) / 2 + 1, e);
}
int Pow(int b, int p) {
  if (!p) return 1;
  long long z = Pow(b, p / 2);
  if (p & 1) return z * z % M * b % M;
  return z * z % M;
}
int calc(int u, int v) {
  if (cur.empty()) return 1;
  ans = cur;
  for (auto &x : ans) x.second = 0;
  while (true) {
    int ch1 = chH[ch[u]];
    int ch2 = chH[ch[v]];
    if (ch1 == ch2) break;
    if (d[ch1] < d[ch2]) swap(u, v);
    ql = eb[chH[ch[u]]];
    qr = ee[u];
    get(1, 1, w);
    u = p[chH[ch[u]]];
  }
  if (d[u] > d[v]) swap(u, v);
  ql = eb[u];
  qr = ee[v];
  get(1, 1, w);
  int res = 1;
  for (auto &x : ans) res = (long long)res * Pow(x.first, x.second) % M;
  return res;
}
int main() {
  memset(ch, -1, sizeof(ch));
  for (int i = 2; i * i <= 10000; ++i)
    if (!notPrime[i])
      for (int j = i * i; j <= 10000; j += i) notPrime[j] = true;
  vector<int> P;
  for (int i = 2; i <= 10000; ++i)
    if (!notPrime[i]) P.push_back(i);
  scanf("%d", &n);
  g.resize(n);
  for (int i = 1, a, b; i < n; ++i) {
    scanf("%d%d", &a, &b);
    --a;
    --b;
    g[a].push_back(b);
    swap(a, b);
    g[a].push_back(b);
  }
  f.resize(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &v[i]);
    int x = v[i];
    for (int j = 0; j < P.size() && P[j] * P[j] <= x; ++j) {
      if (x % P[j] == 0) {
        f[i].push_back({P[j], 0});
        while (x % P[j] == 0) {
          x /= P[j];
          ++f[i].back().second;
        }
      }
    }
    if (x != 1) f[i].push_back({x, 1});
  }
  p[0] = -1;
  val.push_back({0, 0});
  DFS(0);
  build(0);
  w = val.size() - 1;
  build(1, 1, w);
  int q;
  scanf("%d", &q);
  while (q--) {
    int u, v, a;
    scanf("%d%d%d", &u, &v, &a);
    --u;
    --v;
    cur.clear();
    int x = a;
    for (int j = 0; j < P.size() && P[j] * P[j] <= x; ++j) {
      if (x % P[j] == 0) {
        cur.push_back({P[j], 0});
        while (x % P[j] == 0) {
          x /= P[j];
          ++cur.back().second;
        }
      }
    }
    if (x != 1) cur.push_back({x, 1});
    printf("%d\n", calc(u, v));
  }
  return 0;
}