Prime numbers are those numbers which have only 2 factors, 1 and the number itself. For example, 3 is a prime number have factors 1 and 3 only.
Now, all the prime numbers are arranged sequentially in ascending order. i.e:- 2, 3, 5,7...and so on. Now, your task is to calculate those prime numbers which are present at a prime position. For example, 2 is present at position 1 which is not a prime number while 3 is present at position 2 which is a prime number, hence 3 is the first prime positioned number. Now, these primed positioned prime numbers are numbered is ascending order such that:
first prime positioned number is 3. So, it is given number 1.
second prime positioned number is 5. So, it is given number 2.
and so on. Now, we are given 2 numbers n and m which denotes the nth and mth prime positioned numbers. Now, your task is to calculate the multiplication of prime positioned number present at nth and mth position. Since the answer may be large, output the answer modulo 10^9 + 7.
INPUT
First line contains an integer t denoting the number of test cases. The next t lines contains two integers n and m separated by space, denoting the prime positioned numbers which are given numbers n and m.
OUTPUT
A single integer (one per line) for each test t, that is the answer to the question.
CONSTRAINTS
1 ≤ t ≤10^6
1 ≤ n,m ≤10000
SAMPLE INPUT
2
1 2
2 3
SAMPLE OUTPUT
15
55
Explanation
Test case 1: Here n=1 and m=2. Now, 1st and 2nd prime positioned numbers are 3 and 5 respectively. So, answer is 3*5=15.
Test case 2: Here n=2 and m=3. Now, 2nd and 3rd prime positioned numbers are 5 and 11 respectively. So, answer is 55.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse problem: We have prime numbers arranged sequentially: p1=2, p2=3, p3=5, p4=7, p5=11, ... (positions starting from 1). Among these, we consider only those primes that are at a prime position. So positions i that are prime: i = 2,3,5,7,11,... At these positions, the prime numbers are: p2=3, p3=5, p5=11, p7=17, p11=31, ... Then these are enumerated as "prime positioned numbers" in order: first = 3, second = 5, third = 11, fourth = 17, fifth = 31, ... So we define a sequence a_k = the prime at the k-th prime index (i.e., a_k = p_{q_k} where q_k is the k-th prime). Equivalent: a_k is the prime whose index in the list of primes is prime, and we renumber them.
Given n and m (1 ≤ n,m ≤ 10000), we need to compute a_n * a_m mod MOD = 1e9+7.
We have t up to 1e6 test cases. That's huge. So we need to precompute a_k for k up to max(n,m) max 10000. But careful: n,m up to 10000, so we need a_k for k up to 10000. That's the maximum index of the "prime positioned prime" we need. So we need to find the 10000th prime-indexed prime. Let's estimate size: The prime counting function π(x) ~ x/ln x. We need the k-th prime number q_k (the k-th prime) and then we need the prime number at index q_k: p_{q_k}. So roughly: q_k ~ k log k. Then p_{q_k} ~ q_k log q_k ~ (k log k) * log(k log k) = k log k * (log k + log log k) = k (log k)^2 + smaller. For k=10000, log k ~ ln(10000) ≈ 9.21, (log k)^2 ≈ 84.9, so p_{q_k} ~ 10000 * 84.9 ≈ 849,000. Actually natural log. But base e. So about 850k. Not too large. But wait: we need primes up to maybe around million. Actually p_{q_k} is the prime number at index q_k. q_k for k=10000: The 10000th prime is about 10000 * ln(10000) ≈ 100009.21=92100? That's not right: Actually the nth prime ~ n ln n. So q_k ~ k ln k. For k=10000, ln(10000)=9.21, so q_10000 ≈ 100009.21 = 92100. That's the prime index we need. Then p_{92100} is the prime number at position 92100, which is approximately 92100 * ln(92100) ≈ 92100 * 11.43 ≈ 1.052e6. So about 1 million. Actually p_n ~ n ln n. So p_{92100} ~ 92100 * ln(92100) ≈ 92100*11.43 ≈ 1.053e6. So we need primes up to about 1.1 million to get the 10000th prime-indexed prime. That's manageable. We can sieve up to maybe 2 million to be safe (since p_{q_k} might be a bit larger). For k=10000, we can compute exactly.
But we must be careful: The maximum required index for a_k is 10000. That means we need the 10000th prime that sits at prime index. That corresponds to the 10000th prime number in the list of primes whose index is prime. So we need to know the index i such that i is prime and it's the 10000th such. That i is the 10000th prime (the primes themselves as positions). That i is the 10000th prime number. Then we need the i-th prime. So we need to compute the first 10000 primes (to get the positions), and then for each of those primes, we need the prime at that index. So essentially we need two layers of primes. We'll need primes up to some bound such that we have at least the 10000th prime as an index, and then we need primes up to the value of p_{that index}. The maximum prime we will need is p_{q_10000} where q_10000 = the 10000th prime. So we need to compute all primes up to, say, 2 million to be safe. Let's estimate: q_10000 ~ 104729? Actually let's check: The 10000th prime is known: 104729? Wait, I recall the 10000th prime is 104729? Let's verify: I know the 1000th prime is 7919, the 10000th prime is 104729? Actually I think the 10000th prime is 104729. But I'm not 100% sure. Let's check quickly: π(100000) = 9592, so 10000th prime is >100000. Known: 10000th prime is 104729. Yes, I think it is 104729. Actually I recall the 10000th prime is 104729, and 1000th is 7919, 100th is 541, 10th is 29. So q_10000 = 104729. Then p_{104729} is the prime at index 104729. That prime is about 104729 * ln(104729). ln(104729) ≈ 11.56, product ≈ 1.21e6. Actually more precisely, the 104729th prime is around 1,400,000? I recall the 100,000th prime is 1,299,709? Hmm. Let's find: I know the 100,000th prime is 1,299,709, so the 104,729th prime is a bit larger, maybe around 1,400,000. So we may need primes up to about 1.5 million. For safety, we could sieve up to 2 million. That's fine.
But we have up to 1e6 test cases. We need to answer each test by simply looking up precomputed a_n and a_m and multiply modulo MOD. So precomputation: generate list of primes up to some limit N (say 2e6). While generating, we can store primes in an array primes[], and also compute an array is_prime (sieve). Also we need mapping: For each prime index (i.e., for each prime position i), we want the prime at that index. But we don't need all; we need only up to the 10000th such. So we can iterate over primes and track when the index (starting from 1) is prime. We'll generate list a[1..K] where K = 10000. Approach:
Sieve to get all primes up to MAX. We need enough primes to cover p_{q_K}. We need to know the maximum prime index we will query: q_K = the K-th prime, where K=10000. So we need to generate at least q_K primes. That is, we need to have an array of primes with length at least q_K. And also the primes themselves need to go up to p_{q_K}. So we need to sieve up to a bound where the number of primes found >= q_K and the last prime (the q_K-th prime) is known. Then we also need primes up to p_{q_K}. But if we sieve up to a limit L, we get all primes <= L. Then the count of primes π(L) must be >= q_K, and also L must be at least p_{q_K} (since p_{q_K} <= L). So we need to choose L such that the q_K-th prime <= L. So we can just set L generously to, say, 2 million, which is safe. Let's check: π(2e6) is about 2e6/ln(2e6) ≈ 2e6/14.5 ≈ 138,000, so we have at least 138k primes, > 104729, and the 104729th prime is definitely less than 2 million (since the 138kth prime is about 2e6, so 104729th is lower). So 2 million is safe. Actually the 150,000th prime is about 2e6? Let's approximate: p_n ~ n ln n, for n=150k, ln(150k)=12, product = 1.8e6. So yes, 2e6 is safe.
So we can sieve up to 2e6 (or maybe 1.5e6). But we have many test cases, and memory is not an issue; 2e6 boolean array is fine.
After sieve, we generate array primes list. Then we need to find a array A of length Kmax = 10000 where A[i] = primes[ primes[i] - 1 ]? Wait careful: The primes list indexes from 0 or 1? Typically we define primes[1] = 2, primes[2] = 3, etc. But if we store primes starting at index 0 (0-based), then the prime at position i (1-based) is primes[i-1]. The prime index j (1-based) is prime number; so the prime at position j is primes[j-1]. So if we have list primes (0-based) where primes[0]=2, primes[1]=3, etc., then the prime at position j (1-based) is primes[j-1]. The positions that are prime: for j being prime (like 2,3,5,...). Then the prime at that position is primes[j-1]. And we enumerate these in order as a[1], a[2], ... So we need to iterate over indices j starting from 1 to as many as needed, check if j is prime. When we encounter such j, we record primes[j-1] as next element of A. Continue until we have 10000 entries.
We'll need to check primality of j quickly. Since j goes up to what? The largest j we need to consider is the position index of the 10000th prime-indexed prime. That's the 10000th prime number. That is q_10000 ≈ 104729. So we need to know primality of numbers up to about 105k. But we already have sieve up to 2e6, so we can just use the same sieve to check is_prime[j] (for j up to max bound). So we can just loop j from 1 to maybe large while building A.
Implementation steps:
Define MOD = 1e9+7.
Set MAX_N = some upper bound like 2_000_000 (maybe a bit higher to be safe, but 2e6 is fine). Actually to be very safe, we can compute the needed bound more precisely: We need at least the 10000th prime q_10000, and also p_{q_10000}. Could be around 1.4e6. So 2e6 safe.
Use sieve (e.g., Eratosthenes) to generate is_prime array of size MAX_N+1, and primes list (store all primes up to MAX_N). We'll use byte array for speed? Python might be slower with large t. But we need to handle up to 1e6 test cases in Python? t up to 1e6. That's huge; we need efficient I/O and precomputation. Python might struggle with 1e6 lines of input and output, but possible if using sys.stdin.buffer, sys.stdout.write. But we also need to compute multiplication mod and output. That's doable.
But the main challenge: generating primes up to 2 million in Python quickly and then building A. That's fine; sieve of Eratosthenes up to 2 million is quick (maybe 0.1 sec). Then we have list primes (length around 150k). Then we need to build array A of size 10001 (1-indexed) where A[k] is the k-th prime-indexed prime.
We can do:
primes = [] # list of primes in order
is_prime = [True] * (MAX_N+1)
is_prime[0]=is_prime[1]=False
for i in range(2, MAX_N+1):
if is_prime[i]:
primes.append(i)
for j in range(i*i, MAX_N+1, i):
is_prime[j]=False
But note: ii may overflow but we can handle with int. For speed, we might want to use typical optimized sieve like array of bool, and start marking from ii. For 2 million, it's fine.
Alternatively, we can precompute using known values: The sequence of prime-indexed primes is known as "prime-indexed primes" or "superprimes". The first 10000 are known? Could compute offline, but we'll compute.
After primes list is built, we need to generate A. We'll set A = [] or list of length 10001 (index from 1). We'll iterate j from 1 up to while len(A) < 10000. Actually we need to consider j starting from 1. But position 1 is not prime. So start from 2. For each j, if is_prime[j], then the prime at that position is primes[j-1] (since primes[0] is position 1). Append it. Continue until we have 10000.
But note: j will go at least up to the 10000th prime, which is around 104729. That is within MAX_N. And primes[j-1] will be defined because j-1 index in primes list corresponds to the j-th prime (since index 0 -> 1st prime). Since primes list length is > j-1 (since we have enough primes). So it's fine.
Now we have A[1..10000]. Then for each test case, read n and m, compute (A[n] * A[m]) % MOD, output.
Now memory: A stores integers up to maybe 2e6, 10000 entries, fine.
Time: Precomputation O(MAX_N log log MAX_N) ~ few ms in Python? Actually 2e6 sieve might be around 0.1-0.2 sec in Python if optimized, maybe a bit more but acceptable. Then reading 1e6 lines might be the bottleneck. We'll need fast input. Use sys.stdin.buffer.read() to read all and split. That's typical. Output: we need to output t lines. We'll accumulate results in list and join.
But we have up to 10^6 test cases, each result is an integer, plus newline. That's about 8-10 MB output, fine.
Potential issues: The sieve up to 2e6 might be memory heavy but fine: bool array of 2e6+1 bytes ~ 2 MB.
Edge cases: n and m could be up to 10000. They are within precomputed range.
Now note that t can be up to 10^6, but n,m values are within 10000. So we can compute product quickly.
Now confirm maximum needed prime: We need primes up to p_{q_10000}. Let's compute approximate: q_10000 = 104729. p_{104729} is the 104729th prime. According to known data: The 100,000th prime is 1,299,709, and the 105,000th prime is around 1,390,000? Actually I can quickly estimate: The prime number theorem: p_n ≈ n (ln n + ln ln n - 1). For n=104729, ln n = ln(104729) ≈ 11.560, ln ln n ≈ 2.448, so p_n ≈ 104729*(11.560+2.448-1) = 10472912.008 ≈ 1,258,000? That seems low. More precisely: p_n ≈ n (ln n + ln ln n - 1 + (ln ln n -2)/ln n ...). Let's compute: ln n = log(104729)=? Actually natural log: using calculator mental: ln(100000)=11.5129, ln(104729) ~ 11.56. ln ln n = ln(11.56) ≈ 2.447. So ln n + ln ln n -1 = 11.56+2.447-1 = 13.007. Multiply 10472913.007 ≈ 1,362,000? Wait 100k13 = 1.3M, plus 4.7k13=61k, total ~1.361M. So about 1.36 million. So 2 million is safe.
But we also need to ensure that we have primes list of length at least 104729. That's fine.
But we also need is_prime up to at least 104729 to check primality of positions j. That's covered.
Now we need to be careful about indexing: primes list from sieve: typically we generate primes sequentially. So primes[0] = 2 (1st prime). So position i corresponds to primes[i-1]. To check if i is prime, we can use is_prime[i] from sieve. We'll loop i from 2 upward, and when is_prime[i] is True, we take prime_val = primes[i-1] and append to A. Continue until A has 10000 entries.
Now what about performance of sieve with Python for 2e6? Let's test mentally: Simple sieve using list of booleans and marking multiples might be okay. However, marking from ii may cause ii to exceed MAX_N for i > sqrt(MAX_N). So we can break. We can implement typical:
def sieve(n):
is_prime = [True](n+1)
is_prime[0]=is_prime[1]=False
for i in range(2, int(n**0.5)+1):
if is_prime[i]:
step = i
start = ii
is_prime[start:n+1:step] = [False]*(((n - start)//step)+1)
primes = [i for i, val in enumerate(is_prime) if val]
return is_prime, primes
But careful: Using slice assignment with list of False may be efficient. However, we need to ensure we don't allocate large lists repeatedly. Could also use bytearray. But for 2e6, it's fine.
Alternatively, we can use a bitarray for memory but not needed.
Now reading input: t up to 1e6 lines. We'll read all bytes, split, then parse ints. We'll have 2t integers plus t maybe? Actually each line has two integers. So total numbers = 2t. For t=1e6, that's 2e6 ints. Parsing them via map(int, data) after split might be memory heavy but still okay? Splitting the entire input into a list of strings of length ~ 2e6 may use some memory (like 2e6 strings). But each string small, overhead may be high. Could use sys.stdin.buffer.read().split() which returns list of bytes. That could be ~2e6 items. In Python, each bytes object is separate? Actually .split() on bytes returns list of byte strings, each referencing a slice. Overhead might be around 37-50 bytes per object, so 2e6 * 50 = 100 MB, plus the raw data ~ maybe 10 MB. Might be high but still maybe within limits? Usually memory limit maybe 256 MB or 512 MB, might be okay but borderline. Alternative: read line by line with sys.stdin.readline, but that is slower but maybe okay if we use fast I/O? 1e6 lines reading line by line might be okay if each line is small. However, Python's sys.stdin.readline is buffered, and we can process each line, parse two ints using map(int, line.split()). That would avoid storing all at once. But 1e6 lines might be okay timewise if we do simple parsing. However, we must ensure it's not too slow. Typically, reading 1e6 lines with sys.stdin.buffer.readline and splitting can be done in about 1-2 seconds in PyPy. So I think that's acceptable. But we need to output quickly too.
We can combine: Use sys.stdin.buffer.read().split() and then iterate in chunks. That might be faster. But let's evaluate: For 1e6 lines, each line maybe "1 2\n" ~ 4 bytes, total input ~ 4 MB? Actually each line has at least 3 characters plus newline, average maybe 6 characters (since numbers up to 10000, could be 1-5 digits). So roughly 6 MB. Splitting into 2e6 tokens yields tokens of 1-5 bytes each, the list of 2e6 small bytes. That could be memory heavy but not impossible: 2e6 objects, each overhead ~ 49 bytes (on 64-bit CPython) = 98 MB plus data ~ 10 MB, total ~108 MB. Might be acceptable, but could be near limit. However, in PyPy, overhead might be different, maybe more? I'd prefer line-by-line to keep memory low.
Given that t up to 1e6, line-by-line reading using sys.stdin.buffer.readline in a loop may be fine.
Implementation plan:
Precompute MOD = 10**9+7.
Set MAX = 2000000 (maybe 2_000_000). But to be safe maybe 2_000_000.
Perform sieve to get is_prime list and primes list.
is_prime: boolean list of size MAX+1.
For i in range(2, int(sqrt(MAX))+1): if is_prime[i]: mark multiples from i*i.
Then collect primes = [i for i in range(2, MAX+1) if is_prime[i]].
This will create primes list in order.
But careful: We need is_prime for numbers up to maybe 2e6. But also we need primes list to index by position. So primes list must contain all primes up to MAX. That's fine.
Build array A of length 10001 (index 1..10000) as described:
A = [0] # dummy index 0
i = 1 # 1-based index for positions
while len(A) <= 10000:
if is_prime[i]:
# prime at position i is primes[i-1]
A.append(primes[i-1])
i += 1
We need to ensure i does not exceed length of primes. Since we need 10000 entries, i will go to maybe 104729, which is within primes length because primes length is > 104729.
Read t from input.
For each of t lines: parse n and m, compute (A[n] * A[m]) % MOD, store in list of strings or write to output buffer.
Use sys.stdout.write with join.
Potential pitfalls: The indexing: primes[i-1] when i=1 gives primes[0]=2. But i=1 is not prime, so we won't use. So we need to ensure that for i up to maybe 104729, primes[i-1] exists. Since primes list length is about 150k, that's okay.
But what if i becomes larger than length of primes? The while loop runs until we have 10000 A entries. The i needed is the 10000th prime index. That is known to be 104729. So primes list length must be at least 104729. Our sieve up to 2e6 yields around 148933 primes (π(2e6) ≈ 148933). So it's enough.
Now double-check with known values: The superprimes sequence: 3,5,11,17,31,41,59,67,83,109,127,157,179,191,211, ... For k=1,3; k=2,5; k=3,11; k=4,17; k=5,31; k=6,41? Actually let's check: Positions prime: 2->3; 3->5; 5->11; 7->17; 11->31; 13->41; 17->59; 19->67; 23->83; 29->97? Wait 29->? 29th prime is 109? Actually the 29th prime is 109? Let's list: primes: 1:2,2:3,3:5,4:7,5:11,6:13,7:17,8:19,9:23,10:29,11:31,12:37,13:41,14:43,15:47,16:53,17:59,18:61,19:67,20:71,21:73,22:79,23:83,24:89,25:97,26:101,27:103,28:107,29:109,30:113,... So for position 29 (which is prime), prime is 109. So a_? Actually we enumerate only prime positions: position 2 -> a1=3; 3->a2=5;5->a3=11;7->a4=17;11->a5=31;13->a6=41;17->a7=59;19->a8=67;23->a9=83;29->a10=109;... So a6 should be 41? Let's verify: The sample says 2nd and 3rd prime positioned numbers are 5 and 11 => product 15. For test2: n=2,m=3 => 5 and 11 => 55? Wait they said 2nd and 3rd are 5 and 11 => product 55. So a2=5, a3=11. That matches. So a6? Not needed. So our computed A should match.
Now we need to verify that we generate A correctly. For i=2 (prime) -> primes[1]=3 -> append -> A[1]=3. i=3 -> primes[2]=5 -> A[2]=5. i=4 not prime skip. i=5 -> primes[4]=11 -> A[3]=11. i=6 skip. i=7 -> primes[6]=17 -> A[4]=17. i=8,9 skip. i=10 skip? 10 not prime. i=11 -> primes[10]=31 -> A[5]=31. i=12 skip. i=13 -> primes[12]=41 -> A[6]=41. Good.
So algorithm seems correct.
Now performance: Sieve up to 2e6, building primes list and A up to 10000. Then reading input and processing.
But we have to be careful: t up to 1e6, we need to ensure that reading and output are fast. We'll use sys.stdin.buffer.readline in a loop. Example:
import sys
def main():
import sys
input = sys.stdin.buffer.readline
t = int(input())
out_lines = []
for _ in range(t):
line = input()
while line.strip() == b'':
line = input() # skip empty? Not needed.
n, m = map(int, line.split())
res = (A[n] * A[m]) % MOD
out_lines.append(str(res))
sys.stdout.write('\n'.join(out_lines))
But building a list of 1e6 strings might consume memory (each string maybe 10 chars, plus overhead). That could be large: 1e6 strings ~ maybe 50 MB or more? Actually each string object overhead ~ 49 bytes, so 49 MB, plus the characters ~ 10 MB, total ~ 60 MB. Plus other memory may be okay. But we can instead write directly to sys.stdout using write for each result with newline, but that may be slower due to many syscalls. Better to collect in list and join, that's typical. But memory for 1e6 strings could be high. Alternative: we can use an array of bytes and write incrementally with a buffer. But Python's sys.stdout.write with many small writes may be okay if we use .write for each? Possibly not. Usually for large output, we accumulate in a list and then join is recommended because it reduces syscalls. But the list memory might be high.
We can use io.StringIO to build a large string incrementally. That might be similar to list join. Actually joining a list of strings is efficient, and the memory for the final string is about the total size. The list holds references to strings, but strings themselves are separate objects. To reduce memory, we could convert numbers to strings and directly write to a buffer using sys.stdout.write in a loop, but Python's sys.stdout is buffered by default (when writing to a file or terminal, it's line-buffered for terminal but block-buffered for pipes/files). Actually sys.stdout is a buffered stream; write calls add to buffer, and flush when buffer full. So writing per result with newline might be okay as the buffer will handle batching. Usually for 1e6 lines, writing line by line with sys.stdout.write is fine; the buffer size is typically 8192 bytes, so it will flush periodically. It might be slower than join but not too bad. However, I've seen that join is often faster. But memory may be a concern. But 1e6 strings of small lengths: each integer result is up to MOD-1 ~ 1e9, so up to 10 digits. So each string length average maybe 9-10 bytes. So total characters ~10 MB. The list of 1e6 strings will have 1e6 references (8 bytes each) plus string objects overhead. Each string object overhead is about 49 bytes (on CPython 64-bit) plus the character data (allocated separately). So that's about 49 MB for overhead + 10 MB for data = 59 MB. Plus the list itself 8 MB for pointers? Actually the list holds pointers, each 8 bytes, so 8 MB. So total maybe 75 MB. That's okay if memory limit is e.g., 256 MB. So we can do list join.
But we also have other data: is_prime array (2e6 bools -> Python list of bools, each bool is an object? Actually Python bool is a subclass of int, but a list of bools contains references to the singleton True/False objects, so each entry is a pointer (8 bytes) to the True/False object. So a list of 2e6 booleans will take 16 MB for the list (since list overhead: 8 bytes per reference) plus the references themselves. Actually a Python list stores pointers to objects. For bool, True and False are singletons, so the list contains references to these objects, not separate objects. So the list memory is about 8 bytes per element, so 2e6 * 8 = 16 MB, plus list overhead. So is_prime list ~16 MB. primes list: about 150k integers, each integer object ~28 bytes (on 64-bit) plus list pointer overhead. So 150k * 28 ≈ 4.2 MB plus list pointers ~1.2 MB = 5.4 MB. A list of 10001 ints negligible. So total memory maybe 16+5+ <1 + output list 75 MB = ~100 MB. Acceptable.
But note: In Python, a list of bools is inefficient; we could use bytearray or array('b') to reduce memory. But 16 MB is fine. However, if we want to reduce, we can use bytearray for is_prime. That would be 2e6 bytes = 2 MB. That's better. Also for marking sieve, using bytearray is efficient. We'll do that.
We can implement sieve with bytearray:
MAX = 2000000
is_prime = bytearray(b'\x01') * (MAX+1)
is_prime[0] = is_prime[1] = 0
for i in range(2, int(MAX**0.5)+1):
if is_prime[i]:
is_prime[ii:MAX+1:i] = b'\x00' * (((MAX - ii)//i) + 1)
But careful: assigning slice of bytearray with a bytes object of appropriate length is possible. However, constructing b'\x00' * length may create a large bytes object; for i large, length may be big, but overall the total work is similar to standard sieve. Alternatively, we can use a simple loop with range step and assign 0; that might be faster in Python? Actually Python loops are slower; using slice assignment with multiplication is faster. However, creating the repeated bytes for each i may allocate a temporary bytes of size proportional to the count of multiples, which could be large for small i. For i=2, we need to set about 1e6 zeros, creating a bytes of 1e6 bytes; that's okay. For i=3, about 666k, etc. The total memory allocated across all i may be large but temporary and garbage collected. But it's typical and acceptable for 2e6.
But we also need to collect primes. After sieve, we can create primes list by iterating over range(2, MAX+1) and if is_prime[i]: primes.append(i). That's fine.
Now building A: We'll need is_prime for positions up to maybe 105k. That's fine.
Now we must also note that we are asked for output modulo 1e9+7. Multiplication of two numbers up to about 1.5e6 each yields up to ~2.25e12, which fits in Python int; mod operation is fine.
Now we can implement.
But we must verify that the 10000th prime-indexed prime is indeed within our sieve range. Let's approximate: q_10000 = 104729, p_104729 ≈ ? According to known sequence: The super prime numbers: A006450. The 10000th superprime? I'm not sure but it's likely around 1.4 million. So 2 million should be safe. However, I'd like to be more precise to avoid off-by-one. Let's try to compute bound: We need to generate primes up to p_{104729}. I can quickly check known values: The 10000th prime is 104729. The 104729th prime? I can approximate using known prime counts. I know that π(1,300,000) = 100,000? Actually π(1,300,000) is about? The prime count up to 1e6 is 78498, up to 1.2e6 is maybe ~? Let's check from memory: π(1,000,000)=78498, π(1,500,000)=? Actually I can compute roughly: Using approximation π(x) ≈ x / ln x. For x=1.5e6, ln=14.22, π≈105,500. For x=1.6e6, ln≈14.29, π≈111,900. For x=1.7e6, ln≈14.35, π≈118,500. For x=1.8e6, ln≈14.41, π≈124,900. For x=1.9e6, ln≈14.46, π≈131,400. For x=2.0e6, ln≈14.51, π≈137,800. So to have 104,729 primes, we need x such that π(x) ≈ 104,729. That seems around x=1.4-1.5 million? Actually π(1.4e6)=? Ln(1.4e6)=14.15, π≈ 1.4e6/14.15 ≈ 99,000. That seems low. Wait recalc: 1.4e6/14.15 ≈ 98,939. π(1.5e6)=1.5e6/14.22 ≈ 105,500. So to get 104,729, x around 1.49e6. So p_{104729} is about 1.49 million. Actually we need the 104,729th prime, which is approximately the number where π(x)=104,729. That's about 1.5 million. But it could be a bit higher. I've seen that the 100,000th prime is 1,299,709. That's 100,000th prime, so the 104,729th prime will be > 1,299,709. And 1,299,709 is about 1.3 million. So 104,729th prime is maybe around 1.4 million. Let's check known: The 125,000th prime? Not sure. But we can safely set MAX to 2,000,000, which should cover it. Even if it's a bit higher than 2 million, we can increase a bit to 3 million to be safe. But memory for 3 million bytearray is 3 MB, still fine. So we can set MAX = 2_500_000 or 3_000_000 to be extra safe.
Given constraints, we can compute precisely needed bound: We need to generate at least q_10000 = the 10000th prime. That is known to be 104729. Then we need the prime at index 104729, which is the 104729th prime. So we need to generate at least 104729 primes. The sieve bound must be such that π(MAX) >= 104729. And also the 104729th prime must be <= MAX. So we need MAX >= p_{104729}. We can estimate upper bound for p_n: for n>=6, p_n < n (log n + log log n). Actually known bounds: For n >= 6, p_n < n (log n + log log n). (Rosser's theorem). Let's compute for n=104729: log = natural log. log n = ln(104729) ≈ 11.560, log log n ≈ 2.447. So p_n < 104729*(11.560+2.447)=104729*13.007≈1,362,000. So we can set MAX = 1,500,000 to be safe? But the inequality is p_n < n (log n + log log n) for n>=6, but this is an upper bound? Actually Rosser's theorem: p_n < n (log n + log log n) for n >= 6. That gives an upper bound. So p_{104729} < 1,362,000. So MAX=1,500,000 would cover it. But to be absolutely safe and because we are using Python, we can set MAX = 2_000_000. That's fine.
I'll set MAX = 2_000_000.
Now time complexity: Sieve O(MAX log log MAX). With MAX=2e6, it's fast. Building primes list is O(MAX). Then building A: we need to iterate i from 1 until we have 10000 hits. i will go to about 104729. For each i, we check is_prime[i] (O(1)) and if true we append primes[i-1]. That's fine.
Now we need to read t and process.
Potential improvement: Since t up to 1e6, we need to avoid per-test overhead like Python function calls, etc. But it's fine.
Edge Cases: n and m could be equal? Yes, product = a_n^2 mod MOD.
Now we need to ensure we compute modulo multiplication correctly. Since numbers are up to maybe 2 million, product up to 4e12, Python int can handle.
Now test with sample.
Implementation details:
We'll define:
MOD = 10**9+7
def sieve(limit):
is_prime = bytearray(b'\x01') * (limit+1)
is_prime[0:2] = b'\x00\x00'
import math
for i in range(2, int(limit**0.5)+1):
if is_prime[i]:
step = i
start = i*i
is_prime[start:limit+1:step] = b'\x00' * ((limit - start)//step + 1)
primes = []
for i in range(2, limit+1):
if is_prime[i]:
primes.append(i)
return is_prime, primes
But careful: In Python, assignment to slice of bytearray with a bytes object of exactly the needed length works, but we must compute length correctly. Use: is_prime[start:limit+1:step] = b'\x00' * (((limit - start)//step) + 1). This creates a bytes object of length count. This is typical.
Alternatively, we can use memoryview or simple loop, but okay.
Potential memory: The slice assignment with b'\x00' * large number may allocate a large temporary for each i. For i=2, count ~ (limit-4)//2 +1 ≈ (2e6-4)/2 ≈ 1e6, so 1 MB of zeros. For i=3, about 666k, etc. Cumulative allocation might be large but still within memory and time. However, an alternative is to use a for loop with range(start, limit+1, step) and assign 0 to each index; that would be slower due to Python loop overhead. But for limit 2e6, the number of operations for marking multiples is about limit * (1/2 + 1/3 + 1/5 + ...) ~ limit * log log limit ≈ 2e6 * ~2 = 4e6 operations if we do direct assignments in Python loop, which may be okay? Actually Python loop with 4e6 iterations might be okay (0.1 sec?). But each iteration is a Python operation, which may be slower. However, slice assignment uses C-level loops and is much faster. So slice assignment is preferable despite temporary allocations. We can also use memoryview and assign from a pre-allocated zeros array? Not necessary.
We can also use array('b') but similar.
Now one more thing: When we assign is_prime[start:limit+1:step] = b'\x00' * count, we need to compute count. That's fine.
Potential issue: For large limit, i*i may overflow Python int? No, Python int unlimited.
Now after sieve, we have is_prime as bytearray (0/1). For checking is_prime[i], we can do if is_prime[i]: treat as True if non-zero.
Now building A:
K = 10000
A = [0] # index 0 dummy
i = 1
while len(A) <= K:
if is_prime[i]:
A.append(primes[i-1])
i += 1
We need to ensure that i does not exceed len(primes). But since we need only 10000 such, and primes length is > i, fine.
Now we may want to store A as list of ints.
Now reading input:
We'll use sys.stdin.buffer.readline. Since t can be up to 1e6, we need to be careful about the loop overhead. But it's fine.
Alternative: read all bytes and parse ints using map, but memory high. I'll use line-by-line.
Implementation:
import sys
def main():
MOD = 10**9+7
MAX = 2000000
is_prime, primes = sieve(MAX)
K = 10000
A = [0]
i = 1
while len(A) <= K:
if is_prime[i]:
A.append(primes[i-1])
i += 1
code
input = sys.stdin.buffer.readline
t_line = input()
while t_line.strip() == b'':
t_line = input()
t = int(t_line)
out = []
for _ in range(t):
line = input()
while line.strip() == b'':
line = input()
n_str, m_str = line.split()
n = int(n_str); m = int(m_str)
res = (A[n] * A[m]) % MOD
out.append(str(res))
sys.stdout.write('\n'.join(out))
if name == "main":
main()
Now we should test with sample.
But we need to ensure that the sieve function returns both is_prime and primes.
Potential improvement: Instead of generating primes list by iterating over all numbers 2..limit and checking is_prime, we could generate primes during sieve. However, it's fine.
Now we need to check if MAX=2000000 is sufficient. Let's verify that we will indeed get at least 104729 primes. π(2e6) ≈ 148933, so we have enough primes. Also we need that primes[104729-1] exists. Since primes list is zero-indexed, index 104728 corresponds to the 104729th prime. So we need len(primes) > 104728. With π(2e6) ~ 148933, that's fine. Also we need that the prime value at that index (which is p_{104729}) is ≤ MAX. Since MAX=2e6 and p_{104729} < 1.4e6 approx, it's fine.
But to be absolutely sure, we could compute the required MAX by dynamic growth or use known values. I'll just set MAX = 2000000.
Now confirm that A gets built correctly: For i from 1 upward, when i is prime, we take primes[i-1]. Since primes is list of all primes <= MAX, and i up to maybe 104729, primes[i-1] is the i-th prime. So it's fine.
Now potential issue: We used is_prime from sieve; but is_prime[0] and is_prime[1] set to 0. However, in the loop we start i=1, is_prime[1] is 0, fine. For i=2, is_prime[2] should be 1. That works.
Now one more check: When i=2, primes[1] = 3? primes[0]=2, primes[1]=3. So A[1]=3. Good.
Now we might want to precompute all a_n up to 10000 and store them.
Now output for sample: t=2; lines: "1 2" and "2 3". Should output 15 and 55. Our code should produce that.
Now handle large t: The loop for reading lines will run 1e6 times, each with a split and int conversion. That's okay.
But we might want to use map(int, line.split()) for each line. That's fine.
Potential speed: In Python, using split() without arguments splits on any whitespace, which is fine.
Now memory for out list: 1e6 strings. To reduce memory, we could use sys.stdout.write inside the loop but with a buffer. However, we can also accumulate in a list of strings and then join. I'll keep list join.
Alternatively, we can use an array of bytes and write incrementally, but it's okay.
One more thing: The problem statement says: "First line contains an integer t denoting the number of test cases. The next t lines contains two integers n and m separated by space." So t up to 1e6. So we should handle that.
Now we might need to handle the case where input has extra spaces or blank lines? Usually not.
Now we can finalize.
But I'm thinking: maybe we can compute A without needing the full primes list. We could compute primes and then only keep those needed. But we need primes up to index i. So we need primes list anyway.
Now we should also consider if any off-by-one: A[1] should be 3. Verify: i=2 prime, primes[1]=3. So A list length after first append: len(A)=2 (since we started with dummy 0). So A[1]=3. Good.
Now the while condition: while len(A) <= K: we append. So after we have K entries, we stop. So A will have K+1 entries (index 0..K). We'll use A[n] directly.
Now we must ensure that we didn't miss that we need a_n for n up to 10000. So after loop, len(A) = K+1 = 10001. Good.
Now we need to be careful: if the code builds A until len(A) > K, but we break when len(A) > K? Actually condition while len(A) <= K: means we continue while length (including dummy) is <= K. Initially len(A)=1, K=10000, so 1 <= 10000 true, we do iteration. After we append, len becomes 2. We continue until len becomes 10001? Let's test: When len(A) becomes 10001 (i.e., we have added 10000 elements), condition: 10001 <= 10000? false, so loop stops. So final len(A)=10001. So A[10000] is last. Good.
Now performance of sieve: Using bytearray and slice assignment may be efficient. However, the line:
When step is large, the slice length is small, that's fine.
But there is a subtlety: For large limit, constructing a bytes object of length L may take O(L) time and memory, but L decreases as i increases. The total sum of lengths across all i is approximately limit * (1/2 + 1/3 + 1/5 + ... ) ~ limit * log log limit ≈ 2e6 * ~2 = 4e6 bytes of zeros allocated cumulatively? Actually each allocation creates a new bytes object of size L and then assigns it to slice, then the bytes object may be freed. The total amount of allocated memory across all loops is sum of L_i, which is about total number of assignments (i.e., number of composite numbers marked). That's about limit * (1 - 1/2)(1 - 1/3)... Actually the total number of markings is the number of composites that are crossed out. In the sieve, each composite is crossed out once by its smallest prime factor. So total operations is about limit. So total bytes allocated across all loops will be about limit (2e6) plus overhead. Actually each slice assignment creates a bytes object of size equal to number of positions to set. The sum of those sizes over all i is equal to the number of assignments, which is about limit * (1 - 1/2)(1 - 1/3)... Actually it's exactly the number of multiples set to false, but note that each composite may be set multiple times if we set multiples for each prime? Actually in the naive sieve, we mark multiples of each prime starting from ii, but each composite may be marked multiple times if it has multiple prime factors? But in typical optimization, we mark multiples of each prime i from ii, and each composite will be marked at least once, but some may be marked multiple times if they have multiple prime factors? Actually a composite number n will be marked when i is the smallest prime factor of n and ii <= n? Not exactly: When we loop i over primes, we mark multiples j = ii, ii+i, ... For a composite n, it will be marked when i is a prime divisor of n and i <= sqrt(n). But if n has multiple prime divisors, it could be marked multiple times if we mark for each prime i that divides n and i <= sqrt(n). For example, n=30, we will mark when i=2,3,5? But i=5's ii=25, 30 is marked. So it can be marked multiple times. So total markings is sum over primes i of (limit - ii)/i ~ limit * sum 1/p for primes p up to sqrt(limit) which is about limit * (log log limit + M). That's a bit more than limit, but still O(limit log log limit