Chef likes toys. His favourite toy is an array of length N. This array contains only integers. He plays with this array every day. His favourite game with this array is Segment Multiplication. In this game, the second player tells the left and right side of a segment and some modulo. The first player should find the multiplication of all the integers in this segment of the array modulo the given modulus. Chef is playing this game. Of course, he is the first player and wants to win all the games. To win any game he should write the correct answer for each segment. Although Chef is very clever, he has no time to play games. Hence he asks you to help him. Write the program that solves this problem.

-----Input-----
The first line of the input contains an integer N denoting the number of elements in the given array. Next line contains N integers Ai separated with spaces. The third line contains the number of games T. Each of the next T lines contain 3 integers Li, Ri and Mi, the left side of the segment, the right side of segment and the modulo.

-----Output-----
For each game, output a single line containing the answer for the respective segment.

-----Constrdaints-----
- 1 ≤ N ≤ 100,000
- 1 ≤ Ai ≤ 100
- 1 ≤ T ≤ 100,000
- 1 ≤ Li ≤ Ri ≤ N
- 1 ≤ Mi ≤ 109

-----Example-----
Input:
5
1 2 3 4 5
4
1 2 3
2 3 4
1 1 1
1 5 1000000000

Output:
2
2
0
120

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = '# # # # n = int(input())\r\n# # # # arr = list(map(int , input().split()))\r\n# # # # for _ in range(int(input())):\r\n# # # # \tl,r,mod = map(int , input().split())\r\n# # # # \tpro = 1\r\n# # # # \tfor i in range(l - 1,r):\r\n# # # # \t\tpro *= arr[i]\r\n# # # # \tprint(pro % mod) #sample testcases passed #TLE\r\n# # # import numpy #or use math\r\n# # # n = int(input())\r\n# # # arr = list(map(int , input().split()))\r\n# # # for _ in range(int(input())):\r\n# # # \tl,r,mod = map(int , input().split())\r\n# # # \tprint(numpy.prod(arr[l - 1:r]) % mod) #sample cases passed, WA\r\n# # import math\r\n# # primes,dic,t = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],{},0\r\n# # for i in primes:\r\n# #     dic[i] = t\r\n# #     t += 1\r\n# # def primeFactors(n,arr): \r\n# #     for i in range(2,int(math.sqrt(n)) + 1,2): \r\n# #         while(n % i == 0): \r\n# #             arr[dic[i]] += 1 \r\n# #             n /= i\r\n# #     if(n > 2):\r\n# #         arr[dic[n]] += 1\r\n# #     return arr\r\n# # n = int(input())\r\n# # A = list(map(int , input().split()))\r\n# # dp = [0]*len(primes)\r\n# # for i in range(1,n + 1):\r\n# #     r = [dp[i - 1]].copy()\r\n# #     dp.append(primeFactors(A[i - 1],r))\r\n# # for _ in range(int(input())):\r\n# #     li,ri,m=list(map(int,input().split()))\r\n# #     ans = 1\r\n# #     for i in range(len(primes)):\r\n# #         ans *= (pow(primes[i],dp[ri][i] - dp[li - 1][i],m)) % m\r\n# #     print(ans % m) #NZEC\r\n# import math\r\n# primes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\r\n# dic={}\r\n# t=0\r\n# for i in primes:\r\n#     dic[i]=t\r\n#     t+=1\r\n# def primeFactors(n,arr): \r\n#     while(n % 2 == 0):\r\n#         arr[dic[2]] += 1 \r\n#         n /= 2\r\n#     for i in range(3,int(math.sqrt(n))+1,2): \r\n#         while(n % i == 0): \r\n#             arr[dic[i]] += 1 \r\n#             n /= i\r\n#     if(n > 2): \r\n#         arr[dic[n]] += 1\r\n#     return arr\r\n# N = int(input())\r\n# A = list(map(int , input().split()))\r\n# dp = [[0]*len(primes)]\r\n# for i in range(1,N + 1):\r\n#     r = dp[i - 1].copy()\r\n#     dp.append(primeFactors(A[i - 1],r))\r\n# for _ in range(int(input())):\r\n#     l,r,m = list(map(int , input().split()))\r\n#     ans = 1\r\n#     for i in range(len(primes)):\r\n#         ans *= (pow(primes[i],dp[r][i] - dp[l - 1][i],m)) % m\r\n#     print(ans % m)\r\nimport sys \r\nimport math\r\ninput = sys.stdin.readline\r\nprimes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\r\ndic={}\r\nt=0\r\nfor i in primes:\r\n    dic[i]=t\r\n    t+=1\r\ndef primeFactors(n,arr): \r\n    while n % 2 == 0: \r\n        arr[dic[2]]+=1 \r\n        n = n / 2\r\n\r\n    for i in range(3,int(math.sqrt(n))+1,2): \r\n        while n % i== 0: \r\n            arr[dic[i]]+=1 \r\n            n = n / i \r\n\r\n    if n > 2: \r\n        arr[dic[n]]+=1\r\n    return arr\r\ndef main():\r\n    N=int(input())\r\n    A=list(map(int,input().split()))\r\n    tp=[0]*len(primes)\r\n    dp=[]\r\n    dp.append(tp)\r\n    for i in range(1,N+1):\r\n        # print(i)\r\n        r=dp[i-1].copy()\r\n        t=primeFactors(A[i-1],r)\r\n        dp.append(t)\r\n    t=int(input())\r\n    for _ in range(t):\r\n        l,r,m=list(map(int,input().split()))\r\n        if(m==1):\r\n            print(0)\r\n        else:\r\n            ans=1\r\n            for i in range(len(primes)):\r\n                ans=ans*(pow(primes[i],dp[r][i]-dp[l-1][i],m))%m\r\n            print(ans%m)\r\n    \r\ndef __starting_point():\r\n    main()\n__starting_point()'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 16267