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.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.io.*;



public class Main {
    private static final int MAXA = 10*1000*1000;
    private static int [] prime;
    private static ArrayList<Integer> primes;

    private static void sieve(){
        prime = new int[MAXA + 1];
        primes = new ArrayList<>();
        for (int i = 2;i <= MAXA;i++) {
            if (prime[i] == 0) {
                prime[i] = i;
                primes.add(i);
                for (long j = i*(long)i;j <= MAXA;j += i)
                    prime[(int)j] = i;
            }
        }
    }

    private static int n;
    private static ArrayList<Integer> [] G;
    private static int [] A;

    private static void readTree(IO io) throws IOException{
        n = io.getNextInt();
        G = new ArrayList[n+1];
        A = new int[n+1];
        for (int i = 0;i <= n;i++)
            G[i] = new ArrayList<>();

        for (int e = 1;e < n;e++) {
            int a = io.getNextInt(),b = io.getNextInt();
            G[a].add(b);
            G[b].add(a);
        }
        for (int u = 1;u <= n;u++)
            A[u] = io.getNextInt();
    }

    private static int Q;
    private static int [] F,T,X;
    private static int [] ans;

    private static void readQueries(IO io) throws IOException{
        Q = io.getNextInt();
        F = new int[Q];
        T = new int[Q];
        X = new int[Q];
        ans = new int[Q];
        for (int q = 0;q < Q;q++) {
            F[q] = io.getNextInt();
            T[q] = io.getNextInt();
            X[q] = io.getNextInt();
            ans[q] = 1;
        }
    }

    private static ArrayList<Integer> [] nodes,queries;

    private static void register() {
        nodes = new ArrayList[MAXA+1];
        queries = new ArrayList[MAXA+1];


        for (int u = 1;u <= n;u++) {
            int x = A[u];
            while (x > 1) {
                int p = prime[x];
                int q = 1;
                while (x%p == 0) {
                    x /= p;
                    q *= p;
                }
                if (nodes[q] == null)
                    nodes[q] = new ArrayList<>();
                nodes[q].add(u);
            }
        }

        for (int q = 0;q < Q;q++) {
            int x = X[q];
            while (x > 1) {
                int p = prime[x];
                int pe = 1;
                while (x%p == 0) {
                    x /= p;
                    pe *= p;
                }
                if (queries[pe] == null)
                    queries[pe] = new ArrayList<>();
                queries[pe].add(q);
            }
        }
    }

    private static int [] dfs_in,parent;
    private static int dfs_time,h_cnt;
    private static int [] siz,pref_child;
    private static int [] h_id,h_head,h_siz;

    private static void prepareHLD(){
        dfs_time = h_cnt = 0;
        dfs_in = new int[n+1];
        parent = new int[n+1];
        siz = new int[n+1];
        pref_child = new int[n+1];
        h_id = new int[n+1];
        h_head = new int[n+1];
        h_siz = new int[n+1];
    }

    private static void dfs(int u,int p) {
        parent[u] = p;
        siz[u] = 1;
        for (int v : G[u])
            if (v != p) {
                dfs(v,u);
                siz[u] += siz[v];
                if (siz[v] > siz[pref_child[u]])
                    pref_child[u] = v;
            }
    }
    private static void hld(int u,int p) {
        dfs_in[u] = dfs_time++;
        h_id[u] = h_cnt;
        if (h_siz[h_cnt] == 0)
            h_head[h_cnt] = u;
        h_siz[h_cnt]++;

        if (pref_child[u] != 0)
            hld(pref_child[u],u);

        for (int v : G[u])
            if (v != p && v != pref_child[u]) {
                h_cnt++;
                hld(v,u);
            }
    }

    private static int [] cnt,sum;

    private static void buildST(){
        int p2 = 1;
        while (p2 < n) p2 <<= 1;
        p2 <<= 1;
        cnt = new int[p2];
        sum = new int[p2];
    }

    private static void update(int cur,int s,int e,int p,int a,int b) {
        if (s == e) {
            cnt[cur] = a;
            sum[cur] = b;
            return;
        }
        int m = (s + e) >> 1,left = 2*cur + 1,right = left + 1;
        if (p <= m) update(left,s,m,p,a,b);
        else update(right,m+1,e,p,a,b);
        cnt[cur] = cnt[left] + cnt[right];
        sum[cur] = sum[left] + sum[right];
    }

    private static int resA,resB;

    private static void query(int cur,int s,int e,int l,int r) {
        if (l <= s && e <= r) {
            resA += cnt[cur];
            resB += sum[cur];
            return;
        }
        int m = (s + e) >> 1,left = 2*cur + 1,right = left + 1;
        if (r <= m) query(left,s,m,l,r);
        else if (m < l) query(right,m+1,e,l,r);
        else {
            query(left,s,m,l,m);
            query(right,m+1,e,m+1,r);
        }
    }

    private static void query(int a,int b) {
        resA = resB = 0;
        while (h_id[a] != h_id[b]) {
            if (h_id[a] > h_id[b]) {
                int t = a;
                a = b;
                b = t;
            }

            int id = h_id[b],h = h_head[id];
            query(0,0,n-1,dfs_in[h],dfs_in[b]);
            b = parent[h];
        }
        if (dfs_in[a] > dfs_in[b]) {
            int t = a;
            a = b;
            b = t;
        }
        query(0,0,n-1,dfs_in[a],dfs_in[b]);
    }

    private static int mul(int a,int b) {
        long ab = a*(long)b;
        ab %= onebillion7;
        return (int)ab;
    }
    private static int powmod(int a,int p) {
        if (p == 0) return 1;
        int b = 1;
        for (;p > 1;p >>= 1) {
            if (p%2 == 1) b = mul(a,b);
            a = mul(a,a);
        }
        return mul(a,b);
    }

    private static void solve(){
        for (int p : primes) {
            for (long pe = p;pe <= MAXA;pe *= p) {
                if (nodes[(int)pe] != null)
                    for (int u : nodes[(int)pe])
                        update(0,0,n-1,dfs_in[u],1,0);
            }

            for (long pe = p,e = 1;pe <= MAXA;pe *= p,e++) {
                if (nodes[(int)pe] != null)
                    for (int u : nodes[(int)pe])
                        update(0,0,n-1,dfs_in[u],0,(int)e);

                if (queries[(int)pe] != null)
                    for (int q : queries[(int)pe]) {
                        query(F[q],T[q]);
                        int exp = resA*(int)e + resB;
//                        System.err.println(F[q] + " " + T[q] + " <- " + p + "^" + exp + " = " + powmod(p,exp));
                        ans[q] = mul(ans[q],powmod(p,exp));
                    }
            }
            for (long pe = p;pe <= MAXA;pe *= p) {
                if (nodes[(int)pe] != null)
                    for (int u : nodes[(int)pe])
                        update(0,0,n-1,dfs_in[u],0,0);
            }
        }
    }
    public static void main(String[] args) throws Exception {
        IO io = new IO(null, null);
        sieve();
        readTree(io);
        readQueries(io);
        register();
        prepareHLD();
        dfs(1,0);
        hld(1,0);
        buildST();
        solve();
        for (int q = 0;q < Q;q++)
            io.println(ans[q]);

        io.close();
    }

    private static final int onebillion7 = 1000000007;
}



class IO{
    private BufferedReader br;
    private StringTokenizer st;
    private PrintWriter writer;
    private String inputFile,outputFile;

    public boolean hasMore() throws IOException{
        if(st != null && st.hasMoreTokens()) return true;
        if(br != null && br.ready()) return true;
        return false;
    }

    public String getNext() throws FileNotFoundException, IOException{
        while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    public String getNextLine() throws FileNotFoundException, IOException{
        return br.readLine().trim();
    }

    public int getNextInt() throws FileNotFoundException, IOException{
        return Integer.parseInt(getNext());
    }
    public long getNextLong() throws FileNotFoundException, IOException{
        return Long.parseLong(getNext());
    }

    public void print(double x,int num_digits) throws  IOException{
        writer.printf("%." + num_digits + "f" ,x);
    }
    public void println(double x,int num_digits) throws  IOException{
        writer.printf("%." + num_digits + "f\n" ,x);
    }
    public void print(Object o) throws  IOException{
        writer.print(o.toString());
    }

    public void println(Object o) throws  IOException{
        writer.println(o.toString());
    }
    public IO(String x,String y) throws FileNotFoundException, IOException{
        inputFile = x;
        outputFile = y;
        if(x != null) br = new BufferedReader(new FileReader(inputFile));
        else br = new BufferedReader(new InputStreamReader(System.in));
        if(y != null) writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
        else writer = new PrintWriter(new OutputStreamWriter(System.out));
    }

    protected void close() throws IOException{
        br.close();
        writer.close();
    }
    public void outputArr(Object [] A) throws IOException{
        int L = A.length;
        for (int i = 0;i < L;i++) {
            if(i > 0) writer.print(" ");
            writer.print(A[i]);
        }
        writer.print("\n");
    }
}