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 long long MAXN = 3e5 + 10;
const double EPS = 1e-5;
const long long p = 43;
const long long INF = 1e18 + 21;
const long long MOD = 1e9 + 7;
const double pi = acos(-1);
template <typename T>
T sqr(T x) {
  return x * x;
}
int n, pr[30][MAXN], tin[MAXN], tout[MAXN], t = 0, u[MAXN], v[MAXN], q;
long long a[MAXN], x[MAXN];
int resh[(int)1e7 + 10];
map<int, int> ans[MAXN];
vector<int> g[MAXN];
map<int, int> razlx[MAXN], razla[MAXN];
vector<int> was;
set<int> is_spec;
set<int> spec[MAXN];
map<int, map<int, int> > spec_calc;
map<int, int> now_spec;
set<int> need[MAXN];
int now[3200][30];
map<int, int> get_razl(int x) {
  int now = x;
  map<int, int> ans_;
  while (now > 1) {
    ans_[resh[now]]++;
    now /= resh[now];
  }
  return ans_;
}
void count_pr0(int v) {
  was[v] = 1;
  tin[v] = t;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (was[to] == 0) {
      pr[0][to] = v;
      count_pr0(to);
    }
  }
  tout[v] = ++t;
}
bool is_ancestor(int b, int a) {
  return tin[b] <= tin[a] && tout[a] <= tout[b];
}
int lca(int a, int b) {
  if (is_ancestor(a, b)) {
    return a;
  }
  if (is_ancestor(b, a)) {
    return b;
  }
  int x = a;
  for (int i = 29; i > -1; i--) {
    if (!is_ancestor(pr[i][x], b)) {
      x = pr[i][x];
    }
  }
  return pr[0][x];
}
void spec_dfs(int v) {
  was[v] = 1;
  if (is_spec.count(x[v])) {
    now_spec[x[v]]++;
  }
  for (set<int, int>::iterator it = spec[v].begin(); it != spec[v].end();
       it++) {
    spec_calc[*it][v] = now_spec[*it];
  }
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (was[to] == 0) {
      spec_dfs(to);
    }
  }
}
long long my_pow(const long long &x, long long st, const long long &mod) {
  if (st == 0) {
    return 1;
  }
  if (st % 2 == 0) {
    return sqr(my_pow(x, st / 2, mod)) % mod;
  } else {
    return my_pow(x, st - 1, mod) * x % mod;
  }
}
void dfs(int v_) {
  was[v_] = 1;
  for (map<int, int>::iterator it = razla[v_].begin(); it != razla[v_].end();
       it++) {
    for (int i = 1; i < 30; i++) {
      now[it->first][i] += min(i, it->second);
    }
  }
  for (set<int>::iterator it_ = need[v_].begin(); it_ != need[v_].end();
       it_++) {
    int kek = *it_;
    if (v_ == v[kek]) {
      for (map<int, int>::iterator it = razlx[kek].begin();
           it != razlx[kek].end(); it++) {
        ans[kek][it->first] += now[it->first][it->second];
      }
    }
    if (v_ == u[kek]) {
      for (map<int, int>::iterator it = razlx[kek].begin();
           it != razlx[kek].end(); it++) {
        ans[kek][it->first] += now[it->first][it->second];
      }
    }
    if (v_ == lca(v[kek], u[kek])) {
      for (map<int, int>::iterator it = razlx[kek].begin();
           it != razlx[kek].end(); it++) {
        ans[kek][it->first] -= 2 * now[it->first][it->second];
      }
    }
  }
  for (int i = 0; i < g[v_].size(); i++) {
    int to = g[v_][i];
    if (was[to] == 0) {
      dfs(to);
    }
  }
  for (map<int, int>::iterator it = razla[v_].begin(); it != razla[v_].end();
       it++) {
    for (int i = 1; i < 30; i++) {
      now[it->first][i] -= min(i, it->second);
    }
  }
}
int main() {
  for (int i = 2; i < 1e7 + 1; i++) {
    if (resh[i] == 0) {
      for (int j = i; j < 1e7 + 1; j += i) {
        resh[j] = i;
      }
    }
  }
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> u[0] >> v[0];
    u[0]--;
    v[0]--;
    g[u[0]].push_back(v[0]);
    g[v[0]].push_back(u[0]);
  }
  was.clear();
  was.resize(n, 0);
  count_pr0(0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    razla[i] = get_razl(a[i]);
  }
  for (int i = 1; i < 30; i++) {
    for (int j = 0; j < n; j++) {
      pr[i][j] = pr[i - 1][pr[i - 1][j]];
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> u[i] >> v[i] >> x[i];
    u[i]--;
    v[i]--;
    razlx[i] = get_razl(x[i]);
    if (razlx[i].size() == 1 && razlx[i].begin()->second == 1 && x[i] > 3162) {
      is_spec.insert(x[i]);
      spec[u[i]].insert(x[i]);
      spec[v[i]].insert(x[i]);
      spec[lca(u[i], v[i])].insert(x[i]);
    } else {
      need[u[i]].insert(i);
      need[v[i]].insert(i);
      need[lca(u[i], v[i])].insert(i);
    }
  }
  was.clear();
  was.resize(n, 0);
  spec_dfs(0);
  for (int i = 0; i < q; i++) {
    if (razlx[i].size() == 1 && razlx[i].begin()->second == 1 && x[i] > 3162) {
      int sum_st = spec_calc[x[i]][v[i]] + spec_calc[x[i]][u[i]] -
                   2 * spec_calc[x[i]][lca(u[i], v[i])] +
                   (a[lca(v[i], u[i])] == x[i]);
      ans[i][x[i]] = sum_st;
    }
  }
  was.clear();
  was.resize(n, 0);
  dfs(0);
  for (int i = 0; i < q; i++) {
    long long tmp = 1;
    for (map<int, int>::iterator it = ans[i].begin(); it != ans[i].end();
         it++) {
      tmp *= my_pow((long long)it->first, it->second, MOD);
      tmp %= MOD;
    }
    int v_ = lca(u[i], v[i]);
    for (map<int, int>::iterator it = razla[v_].begin(); it != razla[v_].end();
         it++) {
      tmp *= my_pow((long long)it->first, min(it->second, razlx[i][it->first]),
                    MOD);
      tmp %= MOD;
    }
    cout << tmp << "\n";
  }
  return 0;
}