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 inf_int = 1e8;
const long long inf_ll = 1e16;
const double pi = 3.1415926535898;
bool debug = 0;
const int MAXN = 1e5 + 10;
const int LOG = 20;
const int mod = 1e9 + 7;
vector<int> g[MAXN];
int tin[MAXN];
int tout[MAXN];
int timer;
int up[MAXN][LOG];
int h[MAXN];
void dfs(int v, int p) {
  tin[v] = ++timer;
  up[v][0] = p;
  for (int i = 1; i < LOG; ++i) {
    up[v][i] = up[up[v][i - 1]][i - 1];
  }
  for (int to : g[v]) {
    if (to == p) continue;
    h[to] = h[v] + 1;
    dfs(to, v);
  }
  tout[v] = timer;
}
int lca(int a, int b) {
  if (h[a] < h[b]) swap(a, b);
  int dif = h[a] - h[b];
  for (int i = 0; i < LOG; ++i) {
    if (dif & (1 << i)) a = up[a][i];
  }
  assert(h[a] == h[b]);
  if (a == b) return a;
  for (int i = LOG - 1; i >= 0; --i) {
    if (up[a][i] != up[b][i]) {
      a = up[a][i];
      b = up[b][i];
    }
  }
  return up[a][0];
}
const int MX = 1e7 + 5;
int mn[MX];
void sieve() {
  for (int i = 2; i < MX; ++i) {
    if (!mn[i]) {
      mn[i] = i;
      if (1ll * i * i < MX) {
        for (int e = i * i; e < MX; e = e + i) {
          if (!mn[e]) mn[e] = i;
        }
      }
    }
  }
}
int a[MAXN];
int ans[MAXN];
pair<int, int> query[MAXN];
vector<pair<pair<int, int>, int>> add_ver;
vector<pair<pair<int, int>, int>> add_quer;
void process_ver(int v) {
  int x = a[v];
  while (x > 1) {
    int cur = mn[x];
    int cnt = 0;
    while (mn[x] == cur) {
      cnt++;
      x /= cur;
    }
    add_ver.push_back({{cur, cnt}, v});
  }
}
void process_quer(int val, int ind) {
  int x = val;
  while (x > 1) {
    int cur = mn[x];
    int cnt = 0;
    while (mn[x] == cur) {
      cnt++;
      x /= cur;
    }
    add_quer.push_back({{cur, cnt}, ind});
  }
}
struct Fenwick_tree {
  int t[MAXN];
  Fenwick_tree() { memset(t, 0, sizeof t); }
  inline void add(int pos, int val) {
    for (; pos < MAXN; pos |= pos + 1) {
      t[pos] += val;
    }
  }
  inline int get(int pos) {
    int res = 0;
    for (; pos > 0; pos = (pos & (pos + 1)) - 1) {
      res += t[pos];
    }
    return res;
  }
  inline void add(int l, int r, int x) {
    add(r + 1, -x);
    add(l, x);
  }
} tree;
int POW[MX];
int was_added[MAXN];
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(1, 1);
  sieve();
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    process_ver(i);
  }
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    int a, b, x;
    scanf("%d %d %d", &a, &b, &x);
    ans[i] = 1;
    query[i] = {a, b};
    process_quer(x, i);
  }
  for (auto x : add_ver) {
    if (debug)
      cout << x.first.first << " " << x.first.second << " " << x.second << endl;
  }
  sort(add_quer.begin(), add_quer.end());
  sort(add_ver.begin(), add_ver.end());
  POW[0] = 1;
  for (int top_v = 0, top_q = 0;
       top_v < (int(add_ver.size())) && top_q < (int(add_quer.size()));) {
    int mx = max(add_quer[top_q].first.first, add_ver[top_v].first.first);
    while (top_q < (int(add_quer.size())) && add_quer[top_q].first.first < mx)
      ++top_q;
    while (top_v < (int(add_ver.size())) && add_ver[top_v].first.first < mx)
      ++top_v;
    if (top_q == (int(add_quer.size())) || top_v == (int(add_ver.size())) ||
        add_quer[top_q].first.first != mx || add_ver[top_v].first.first != mx)
      continue;
    int l = top_v, r = top_v;
    while (r < (int(add_ver.size())) && add_ver[r].first.first == mx) ++r;
    int mx_power = 0;
    int POW_TOP = 0;
    while (top_q < (int(add_quer.size())) &&
           add_quer[top_q].first.first == mx) {
      int cur_power = add_quer[top_q].first.second;
      if (cur_power > mx_power) {
        for (int i = l; i < r; ++i) {
          if (add_ver[i].first.second > mx_power) {
            int v = add_ver[i].second;
            int temp = min(add_ver[i].first.second, cur_power) - mx_power;
            tree.add(tin[v], tout[v], temp);
            was_added[v] += temp;
          }
        }
        mx_power = cur_power;
      }
      int id = add_quer[top_q].second;
      int a, b;
      tie(a, b) = query[id];
      int res = 0;
      int lc = lca(a, b);
      res = tree.get(tin[a]) + tree.get(tin[b]) - tree.get(tin[lc]);
      if (lc != 1) res = res - tree.get(tin[up[lc][0]]);
      while (POW_TOP < res) {
        ++POW_TOP;
        POW[POW_TOP] = (1ll * POW[POW_TOP - 1] * mx) % mod;
      }
      assert(res < MX);
      if (debug) cout << "val " << mx << " quer " << id << " " << res << endl;
      ans[id] = (1ll * POW[res] * ans[id]) % mod;
      ++top_q;
    }
    for (int i = l; i < r; ++i) {
      int v = add_ver[i].second;
      tree.add(tin[v], tout[v], -was_added[v]);
      was_added[v] = 0;
    }
    top_v = r;
  }
  for (int i = 1; i <= q; ++i) {
    printf("%d\n", ans[i]);
  }
}
signed main() {
  int t = 1;
  while (t--) solve();
}