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 JAVA):

Solution:
import java.io.*;
import java.util.*;

public class Test {

    static int readInt() {
        int ans = 0;
        boolean neg = false;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c == '-') {
                    start = true;
                    neg = true;
                    continue;
                } else if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return neg ? -ans : ans;
    }

    static long readLong() {
        long ans = 0;
        boolean neg = false;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c == '-') {
                    start = true;
                    neg = true;
                    continue;
                } else if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return neg ? -ans : ans;
    }

    static String readString() {
        StringBuilder b = new StringBuilder();
        try {
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c < '0' || c > '9') {
                    break;
                } else {
                    b.append((char) (c));
                }
            }
        } catch (IOException e) {
        }
        return b.toString().trim();
    }

    static PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    List<Integer>[] g;
    final long M = 1_000_000_007;
    int n, h;
    int[] par;
    int[] level;
    int[][] lca;
    int[] ps = new int[10_000_001];
    int[] qu, qv;
    int[] qlca;
    int[] start, end;
    int pos = 0;
    int[] fen;
    Map<Integer, List<int[]>> fs;

    int fget(int i) {
        int ans = fen[0];
        while (i > 0) {
            ans += fen[i];
            i -= i & (-i);
        }
        return ans;
    }

    void fmod(int i, int d) {
        if (i == 0) {
            fen[i] += d;
            return;
        }
        while (i < fen.length) {
            fen[i] += d;
            i += i & (-i);
        }
    }

    void dfs(int p, int u, int lvl) {
        par[u] = p;
        level[u] = lvl;
        start[u] = pos++;
        for (int v : g[u])
            if (v != p) dfs(u, v, lvl + 1);
        end[u] = pos;
    }

    void buildLCA() {
        for (int i = 0; i < n; i++) lca[i][0] = par[i];
        for (int j = 1; j < h; j++)
            for (int i = 0; i < n; i++) {
                int p = lca[i][j - 1];
                if (p != -1) lca[i][j] = lca[p][j - 1];
            }
    }

    int getLCA(int u, int v) {
        if (u == v) return u;
        if (level[u] < level[v]) {
            int t = u;
            u = v;
            v = t;
        }
        for (int b = h - 1; b >= 0; b--)
            if (level[u] - (1 << b) >= level[v]) u = lca[u][b];
        if (u == v) return u;
        for (int b = h - 1; b >= 0; b--)
            if (lca[u][b] != lca[v][b]) {
                u = lca[u][b];
                v = lca[v][b];
            }
        return lca[u][0];
    }

    int fact(int x, int[][] f) {
        int cnt = 0;
        while (ps[x] != 0) {
            int p = ps[x];
            int c = 0;
            while (x % p == 0) {
                c++;
                x /= p;
            }
            f[cnt][0] = p;
            f[cnt][1] = c;
            cnt++;
        }
        if (x > 1) {
            f[cnt][0] = x;
            f[cnt][1] = 1;
            cnt++;
        }
        return cnt;
    }

    long powmod(int f, int e) {
        long ans = 1;
        int b = Integer.highestOneBit(e);
        for (; b > 0; b >>>= 1) {
            ans = (ans * ans) % M;
            if ((b & e) != 0) ans = (ans * f) % M;
        }
        return ans;
    }

    void start() {
        n = readInt();
        start = new int[n];
        end = new int[n];
        fen = new int[n];
        par = new int[n];
        Arrays.fill(par, -1);
        level = new int[n];
        lca = new int[n][Integer.numberOfTrailingZeros(Integer.highestOneBit(n)) + 1];
        for (int i = 0; i < n; i++) Arrays.fill(lca[i], -1);
        g = new List[n];
        for (int i = 0; i < n; i++) g[i] = new ArrayList<>();
        for (int i = 0; i < n - 1; i++) {
            int u = readInt(), v = readInt();
            u--;
            v--;
            g[u].add(v);
            g[v].add(u);
        }
        dfs(-1, 0, 0);
        int lvl = 0;
        for (int i = 0; i < n; i++) lvl = Math.max(lvl, level[i]);
        while ((1 << h) <= n) h++;
        buildLCA();
        for (int i = 2; i < ps.length; i++)
            if (ps[i] == 0)
                for (int j = i + i; j < ps.length; j += i) ps[j] = i;
        fs = new HashMap<>();
        int[][] fa = new int[10][2];
        for (int i = 0; i < n; i++) {
            int a = readInt();
            int c = fact(a, fa);
            for (int j = 0; j < c; j++) {
                int f = fa[j][0], e = fa[j][1];
                fs.computeIfAbsent(f, x -> new ArrayList<>()).add(new int[]{e, i});
            }
        }
        int q = readInt();
        qu = new int[q];
        qv = new int[q];
        qlca = new int[q];
        for (int i = 0; i < q; i++) {
            int u = readInt(), v = readInt(), x = readInt();
            u--;
            v--;
            qu[i] = u;
            qv[i] = v;
            qlca[i] = getLCA(u, v);
            int c = fact(x, fa);
            for (int j = 0; j < c; j++) {
                int f = fa[j][0], e = fa[j][1];
                for (int k = 1; k <= e; k++)
                    fs.computeIfAbsent(f, y -> new ArrayList<>()).add(new int[]{k, ~i});
            }
        }
        long[] ans = new long[q];
        Arrays.fill(ans, 1);
        for (int p : fs.keySet()) {
            List<int[]> l = fs.get(p);
            Collections.sort(l, (a, b) -> {
                if (a[0] == b[0]) return b[1] - a[1];
                else return b[0] - a[0];
            });
            for (int[] a : l) {
                int i = a[1];
                if (i >= 0) {
                    fmod(start[i], 1);
                    fmod(end[i], -1);
                } else {
                    i = ~i;
                    int u = qu[i], v = qv[i], r = qlca[i];
                    int c = fget(start[u]) + fget(start[v]) - fget(start[r]);
                    if (r > 0) c -= fget(start[par[r]]);
                    ans[i] = (ans[i] * powmod(p, c)) % M;
                }
            }
            for (int[] a : l) {
                int i = a[1];
                if (i >= 0) {
                    fmod(start[i], -1);
                    fmod(end[i], 1);
                }
            }
        }
        for (int i = 0; i < q; i++) writer.println(ans[i]);
    }

    public static void main(String[] args) {
        Test te = new Test();
        te.start();
        writer.flush();
    }
}