Finally, Little Q gets his weapon at level $$$10^5$$$ in the RPG game, now he is trying to beat the boss as soon as possible. The boss has $$$H$$$ units of health point (HP), Little Q needs to cause at least $$$H$$$ units of damage to beat the boss.
Little Q has learnt $$$n$$$ skills, labeled by $$$1,2,\dots,n$$$. Each skill can not be used multiple times, because there is not enough time for Little Q to wait for the skill to cool down. Assume Little Q uses the $$$i$$$-th skill at the $$$x$$$-th frame, the actor controlled by him will take $$$t_i$$$ frames to perform, which means Little Q will not be allowed to use other skills until the $$$(x+t_i)$$$-th frame. The length of the damage sequence of the $$$i$$$-th skill is $$$len_i$$$, which means the skill will cause $$$d_{i,j}$$$ ($$$0\leq j \lt len_i$$$) units of damage at the $$$(x+j)$$$-th frame if Little Q uses the $$$i$$$-th skill at the $$$x$$$-th frame. Note that $$$len_i$$$ can be greater than $$$t_i$$$, for example, the burning skill can burn the boss for a long period, but takes a little time to cast the fire.
The game starts at the $$$0$$$-th frame. Your task is to help Little Q beat the boss as soon as possible, or determine Little Q can't beat the boss using all the skills at most once.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$), the number of test cases. For each test case:
The first line contains two integers $$$n$$$ and $$$H$$$ ($$$1 \leq n \leq 18$$$, $$$1\leq H\leq 10^{18}$$$), denoting the number of skills and the HP of the boss.
For each skill, the first line contains two integers $$$t_i$$$ and $$$len_i$$$ ($$$1 \leq t_i,len_i\leq 100\,000$$$), the second line contains $$$len_i$$$ integers $$$d_{i,0},d_{i,1},\dots,d_{i,len_i-1}$$$ ($$$1\leq d_{i,j}\leq 10^9$$$).
It is guaranteed that the sum of all $$$len_i$$$ is at most $$$3\,000\,000$$$, and there are at most $$$5$$$ cases such that $$$n \gt 10$$$.
For each test case, output a single line containing an integer, denoting the first frame to beat the boss. If it is impossible to beat the boss, please print "-1" instead.
3 1 100 5 3 50 60 70 2 100 2 3 40 40 100 100 2 20 5 1 1000 1 1 999
1 2 -1
An immortal dragon attacked the Byteland. After days of battle, the heroes finally beat the dragon. However, the dragon is immortal such that it can not be killed, so the magicians in Byteland decide to seal the dragon using ancient black magic.
The dragon is scheduled to be sealed under an undirected magic tree with $$$n$$$ vertices. In each vertice of the tree, there are two gems. Each gem has its magic value $$$m$$$ and power value $$$p$$$. The black magic requires removing exactly one gem from each vertex such that there will be exactly one gem remaining at each vertex. The total power is the sum of the power values of all the gems that remained in the tree.
To escape from sealed, the dragon has a chance to attack. It will choose a target vertex on the tree, then select some vertices among it and its neighbor vertices, and finally start an attack on them. If at least one vertex is attacked, and the bitwise XOR sum among magic values of all the gems in attacked vertices is equal to zero, the tree will be broken, and the dragon escapes. The magicians must never let this happen.
Please write a program to help them determine how to choose gems such that the dragon can never escape, and the total power is maximized.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 10$$$), the number of test cases. For each test case:
The first line contains a single integer $$$n$$$ ($$$2\leq n\leq 60$$$), denoting the number of vertices.
In the next $$$n$$$ lines, the $$$i$$$-th line contains four integers $$$m_i$$$, $$$p_i$$$, $$$m_i'$$$ and $$$p_i'$$$ ($$$1\leq m_i,m_i' \lt 2^{64}$$$, $$$1\leq p_i,p_i'\leq 10^7$$$), describing the two gems in the $$$i$$$-th vertex.
Each of the following $$$n-1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \leq u_i,v_i\leq n$$$, $$$u_i\neq v_i$$$), describing an undirected edge between the $$$u_i$$$-th vertex and the $$$v_i$$$-th vertex.
It is guarenteed that the input is a tree.
For each test case, output a single line containing an integer, denoting the total power to seal the dragon. When it is impossible to seal the dragon, please print "-1" instead.
2 2 3 1 3 1 3 2 3 2 1 2 3 1 3 2 1 2 2 4 3 1 3 3 2 1 2 2 3
-1 8
The annual kart race will soon be held in Byteland. The map of the race consists of $$$n$$$ different intersections and $$$m$$$ one-way streets. These intersections are labeled from $$$1$$$ to $$$n$$$, the $$$i$$$-th of which is located at $$$(x_i,y_i)$$$. There are no cycles in the street network, one can only drive to some intersections with the strictly larger value of x-coordinate. The streets may only intersect at the intersections.
The race will start at the $$$1$$$-st intersection and will finish at the $$$n$$$-th intersection. The racers can pick their routes themselves, but they can only drive along the streets marked on the map. It is guaranteed that one can reach any place from $$$1$$$, and any place can reach $$$n$$$, so any route is valid.
The kart race attracts so many sponsors. Each intersection has a slot to set a banner, if you choose to set a banner at the $$$i$$$-th intersection, the race company will get $$$w_i$$$ profits. You are a middleman in the race company, your job is to choose some intersections to set banners such that the total profits are maximized. You know that no racer is willing to see more than a banner, so for every possible route from $$$1$$$ to $$$n$$$, you should guarantee that at most one intersection is chosen.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 1\,000$$$), the number of test cases. For each test case:
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 100\,000$$$, $$$1\leq m\leq 2n$$$), denoting the number of intersections and the number of one-way streets.
In the next $$$n$$$ lines, the $$$i$$$-th line contains three integers $$$x_i$$$, $$$y_i$$$ and $$$w_i$$$ ($$$0\leq x_i,y_i\leq 10^9$$$, $$$1\leq w_i\leq 10^9$$$), describing the $$$i$$$-th intersection. It is guaranteed that no two intersections share the same coordinator.
Each of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n$$$, $$$x_{u_i} \lt x_{v_i}$$$), denoting a one way street from $$$u_i$$$ to $$$v_i$$$. It is guaranteed that each pair of $$$u_i$$$ and $$$v_i$$$ will be described at most once.
It is guaranteed that the sum of all $$$n$$$ is at most $$$1\,500\,000$$$.
For each test case, print a single integer in the first line, denoting the maximum total profits. Then print a sequence of integers in the second line, denoting the indexes of intersections you choose to set a banner. If there are multiple optimal solutions, you should print the lexicographically smallest one.
2 6 6 0 1 1 2 2 1 1 0 1 1 2 1 2 0 1 3 1 1 1 4 3 5 2 6 5 6 1 3 4 2 2 1 0 0 8 1 1 9 1 2
2 2 3 9 2
The museum in Byteland has plenty of jewels on display, secured by $$$n$$$ laser alarms. Each laser alarm can be considered as a segment in the 3D space. In this task, your job is to test the quality of the laser alarm system. You need to find a plane such that it touches the most laser alarms. Note that if the plane touches the endpoint of a segment, it should also be counted.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 10$$$), the number of test cases. For each test case:
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 50$$$), denoting the number of laser alarms.
Each of the following $$$n$$$ lines contains six integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$, $$$x'_i$$$, $$$y'_i$$$ and $$$z'_i$$$ ($$$1 \leq x_i,y_i,z_i,x'_i,y'_i,z'_i \leq 100$$$), describing a segment that connects $$$(x_i,y_i,z_i)$$$ and $$$(x_i',y_i',z_i')$$$. It is guaranteed that the two endpoints of each segment do not coincide.
For each test case, output a single line containing an integer, denoting the maximum possible number of laser alarms that can be touched.
1 4 1 1 1 1 1 2 1 1 10 1 1 11 1 10 1 1 10 2 10 1 1 10 1 2
3
Leshphon was a peaceful and beautiful village in Byteland until the nasty disease came here. There are $$$n$$$ districts in Leshphon, connected by $$$m$$$ directed roads such that you can reach every other district from any district via these directed roads. In other words, it is strongly connected.
To control the sources of infection and cut off the channels of transmission, the government of Byteland decides to close exactly three directed roads in Leshphon, such that the village is not strongly connected anymore. There may be many possible solutions, can you figure out the total number of them?
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 10$$$), the number of test cases. For each test case:
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3\leq n\leq 50$$$, $$$3\leq m\leq n(n-1)$$$), denoting the number of districts and the number of roads.
Each of the following $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \leq u_i,v_i\leq n$$$, $$$u_i\neq v_i$$$), describing a directed road from the $$$u_i$$$-th district to the $$$v_i$$$-th district.
It is guaranteed that the village is strongly connected, and all the given $$$m$$$ roads are pairwise different.
For each test case, output a single line containing an integer, denoting the number of solutions.
1 4 7 1 2 2 3 3 4 4 1 1 3 2 4 3 1
34
You are given $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$ and an integer $$$k$$$. You are asked to distribute these $$$n$$$ integers into $$$k$$$ subsets of equal size such that there are no two equal elements in the same subset.
A subset is a group of integers that appear in the array with no particular order. A subset's incompatibility is the difference between the maximum and minimum elements in that array. You need to report the minimum possible sum of incompatibilities of the $$$k$$$ subsets after distributing the numbers optimally, or determine it is impossible.
The input contains only a single case.
The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k\leq n \leq 70$$$, $$$n\bmod k=0$$$), denoting the number of integers and the parameter $$$k$$$.
The second line contains $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$ ($$$1\le i \le n,\ 1 \leq a_i\leq 10^9$$$), denoting the value of each integer.
Print a single line containing an integer, the minimum possible sum of incompatibilities of the $$$k$$$ subsets after distributing the numbers optimally. If there is no solution, print "-1" instead.
4 2 1 2 1 4
4
8 4 6 3 8 1 3 1 2 2
6
6 3 5 3 3 6 3 3
-1
Little Q's factory recently purchased $$$n$$$ pieces of new equipment, labeled by $$$1,2,\dots,n$$$.
There are $$$n$$$ workers in the factory, labeled by $$$1,2,\dots,n$$$. Each worker can be assigned to no more than one piece of equipment, and no piece of equipment can be assigned to multiple workers. If Little Q assigns the $$$i$$$-th worker to the $$$j$$$-th piece of equipment, they will bring $$$a_i+b_j$$$ profits. However, these workers are not so experienced, there are $$$m$$$ extra constraints. In each constraint, you will be given two integers $$$u_i$$$ and $$$v_i$$$, denoting the $$$u_i$$$-th worker can't be assigned to the $$$v_i$$$-th piece of equipment.
Now please for every $$$k$$$ ($$$1\leq k\leq n$$$) find $$$k$$$ pairs of workers and pieces of equipment, then assign workers to these pieces of equipment, such that the total profits for these $$$k$$$ pairs are maximized, or determine it is impossible.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 200$$$), the number of test cases. For each test case:
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 4\,000$$$, $$$1\leq m\leq 10\,000$$$), denoting the number of workers/pieces of new equipment and the number of constraints.
The second line contains $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$ ($$$1\leq a_i\leq 10^9$$$).
The third line contains $$$n$$$ integers $$$b_1,b_2,\dots,b_n$$$ ($$$1\leq b_i\leq 10^9$$$).
Each of the following $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1\leq u_i,v_i\leq n$$$), denoting the $$$u_i$$$-th worker can't be assigned to the $$$v_i$$$-th piece of equipment. Each pair of $$$u_i$$$ and $$$v_i$$$ will be described at most once.
It is guaranteed that there will be at most $$$10$$$ test cases such that $$$n \gt 100$$$.
For each test case, output $$$n$$$ lines, the $$$k$$$-th ($$$1\leq k\leq n$$$) of which containing an integer, denoting the maximum possible total profits for $$$k$$$ pairs of workers and pieces of equipment. Note that if it is impossible to find such $$$k$$$ pairs, print "-1" at this line.
2 4 4 10 20 30 40 5 10 7 8 4 2 3 2 1 4 1 2 2 2 1 1 1 1 1 1 1 2
48 85 115 130 2 -1
You will be given $$$n$$$ noblesse code pairs $$$(a_1,b_1),(a_2,b_2),\dots,(a_n,b_n)$$$ and $$$q$$$ queries. In each query, you will be given a pair $$$(A,B)$$$, you need to figure out how many noblesse code pairs can be transformed from the given pair $$$(A,B)$$$. Every time you can transform the current pair $$$(A,B)$$$ into $$$(A+B,B)$$$ or $$$(A,A+B)$$$. You can do the transform operation for arbitrary times (or do nothing).
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$), the number of test cases. For each test case:
The first line of the input contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n,q \leq 50\,000$$$), denoting the number of noblesse code pairs and the number of queries.
In the next $$$n$$$ lines, the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1\leq a_i,b_i\leq 10^{18}$$$), describing the $$$i$$$-th noblesse code pair.
In the next $$$q$$$ lines, the $$$i$$$-th line contains two integers $$$A$$$ and $$$B$$$ ($$$1\leq A,B\leq 10^{18}$$$), describing the pair in the $$$i$$$-th query.
It is guaranteed that the sum of all $$$n$$$ is at most $$$500\,000$$$, and the sum of all $$$q$$$ is at most $$$500\,000$$$.
For each query, print a single line containing an integer, denoting the number of noblesse code pairs that can be transformed from the given pair. Note that two noblesse code pairs $$$(a_i,b_i),(a_j,b_j)$$$ are considered to be different if and only if $$$i\neq j$$$.
2 3 4 6 9 5 3 1 1 6 3 1 2 2 1 5 3 2 2 7 14 7 14 7 7 7 14
1 0 1 1 2 2
Little Q likes online shopping very much. In the next $$$10^9$$$ days, there will be $$$n$$$ packages delivered to the post office in total. Let's label the next $$$10^9$$$ days as day 1, day 2, $$$\dots$$$, day $$$10^9$$$ respectively. For the $$$i$$$-th package, it will arrive at the post office at day $$$l_i$$$, and the deadline to take it back home is day $$$r_i$$$, which means Little Q can take it back home at day $$$x$$$ if and only if $$$l_i\leq x\leq r_i$$$.
Every time Little Q comes to the post office, he can take at most $$$k$$$ packages together back home at the same time. Note that Little Q can go to the post office multiple times during a single day. Please help Little Q determine how to take these $$$n$$$ packages back home such that the number of times he will go to the post office is minimized.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 3\,000$$$), the number of test cases. For each test case:
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k\leq n \leq 100\,000$$$), denoting the number of packages and the number of packages Little Q can carry at the same time.
Each of the following $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \leq l_i\leq r_i \leq 10^9$$$), describing a package.
It is guaranteed that the sum of all $$$n$$$ is at most $$$1\,000\,000$$$.
For each test case, output a single line containing an integer, denoting the minimum possible number of times that Little Q will go to the post office.
1 4 2 1 3 2 4 6 7 4 7
2
You are given a directed acyclic graph with $$$n$$$ vertices and $$$m$$$ edges. The vertices are labeled by $$$1,2,\dots,n$$$, and the edges are labeled by $$$1,2,\dots,m$$$.
You will be given $$$q$$$ queries. In the $$$i$$$-th query, you will be given four integers $$$u_i$$$, $$$v_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq m$$$). You need to answer whether vertex $$$u_i$$$ can reach vertex $$$v_i$$$ when only edges labeled by $$$k$$$ ($$$l_i\leq k\leq r_i$$$) are available.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 10$$$), the number of test cases. For each test case:
The first line contains three integers $$$n,m$$$ and $$$q$$$ ($$$2 \leq n \leq 50\,000$$$, $$$1\leq m\leq 100\,000$$$, $$$1\leq q\leq 50\,000$$$), denoting the number of vertices, the number of edges, and the number of queries.
Each of the following $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1\leq u_i \lt v_i\leq n$$$), denoting a directed edge from vertex $$$u_i$$$ to vertex $$$v_i$$$.
In the next $$$q$$$ lines, the $$$i$$$-th line contains four integers $$$u_i$$$, $$$v_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$1\leq u_i \lt v_i\leq n$$$, $$$1\leq l_i\leq r_i\leq m$$$), describing the $$$i$$$-th query.
For each query, print a single line. If vertex $$$u_i$$$ can reach vertex $$$v_i$$$ when only edges labeled by $$$k$$$ ($$$l_i\leq k\leq r_i$$$) are available, print "YES". Otherwise, print "NO".
1 5 6 5 1 2 1 3 3 4 2 4 2 5 3 5 3 5 1 5 3 5 1 6 1 4 1 6 1 4 2 3 1 4 4 5
NO YES YES YES NO
There are $$$n$$$ ancient Greek maps describing the fabled islands Atlantis. The maps are labeled by $$$1,2,\dots,n$$$. The $$$i$$$-th map shows the rectangle area $$$R_i$$$ is a part of Atlantis. The sides of all rectangles are parallel to the axes. There may be multiple islands, and the rectangles may overlap.
Unfortunately, some maps are even unreliable so they will not be considered. You will be given $$$q$$$ queries. In the $$$i$$$-th query, you will be given two integers $$$s_i$$$ and $$$t_i$$$ ($$$1\leq s_i\leq t_i\leq n$$$). Please write a program to figure out the total area of Atlantis when only maps labeled by $$$k$$$ ($$$s_i\leq k\leq t_i$$$) are reliable.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 3$$$), the number of test cases. For each test case:
The first line of the input contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n,q \leq 100\,000$$$), denoting the number of maps and the number of queries.
In the next $$$n$$$ lines, the $$$i$$$-th line contains four integers $$$xa_i$$$, $$$ya_i$$$, $$$xb_i$$$ and $$$yb_i$$$ ($$$0\le xa_i \lt xb_i\leq 10^9$$$, $$$0\le ya_i \lt yb_i\leq 10^9$$$), describing the $$$i$$$-th map $$$R_i$$$. $$$(xa_i,ya_i)$$$ is the southwest corner of $$$R_i$$$, and $$$(xb_i,yb_i)$$$ is the northeast corner of $$$R_i$$$.
In the next $$$q$$$ lines, the $$$i$$$-th line contains two integers $$$s_i$$$ and $$$t_i$$$ ($$$1\leq s_i\leq t_i\leq n$$$), describing the $$$i$$$-th query.
It is guaranteed that all the values of $$$xa_i$$$, $$$ya_i$$$, $$$xb_i$$$, $$$yb_i$$$, $$$s_i$$$ and $$$t_i$$$ are chosen uniformly at random from integers in their corresponding ranges. The randomness condition does not apply to the sample test case, but your solution must pass the sample as well.
For each query, print a single line containing an integer, denoting the total area using the information of all reliable maps in this query.
1 3 6 10 10 20 20 12 12 22 22 15 15 25 25 1 1 2 2 3 3 1 2 2 3 1 3
100 100 100 136 151 187
There are $$$m \times m$$$ cells on a grid, the top-left cell is at $$$(1,1)$$$ while the bottom-right cell is at $$$(m,m)$$$. Initially, all the cells were colored white. Little Q has drawn $$$n$$$ black $$$w\times h$$$ rectangles on the grid. For the $$$i$$$-th rectangle, Little Q chose a cell at $$$(a_i,b_i)$$$, and painted all the cells $$$(x,y)$$$ black, where $$$a_i\leq x\leq a_i+w-1$$$ and $$$b_i\leq y\leq b_i+h-1$$$.
After Little Q finished all of his work, he is now wondering how many pairs of white cells are 4-connected. Please write a program to calculate: $$$$$$\sum_{(i,j)|1\leq i,j\leq m,\ (i,j)\text{ is white}}f(i,j)$$$$$$
Here $$$f(i,j)$$$ is the number of white cells that are 4-connected with $$$(i,j)$$$, including $$$(i,j)$$$ itself.
Two cells are considered adjacent if and only if they share a common side. Two white cells $$$(i,j)$$$, $$$(x,y)$$$ are considered 4-connected if and only if there exists a sequence of white cells $$$c_1,c_2,\dots,c_k$$$ such that:
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 1\,000$$$), the number of test cases. For each test case:
The first line contains four integers $$$n$$$, $$$m$$$, $$$w$$$ and $$$h$$$ ($$$1 \leq n \leq 100\,000$$$, $$$1\leq w,h\leq m\leq 10^9$$$), denoting the number of rectangles, the size of the grid, and the size of each rectangle.
Each of the next $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1\leq a_i\leq m-w+1$$$, $$$1\leq b_i\leq m-h+1$$$), denoting a rectangle.
It is guaranteed that the sum of all $$$n$$$ is at most $$$2\,000\,000$$$.
For each test case, print a single line containing an integer denoting the answer. Note that the answer may be extremely large, so please print it modulo $$$2^{64}$$$ instead.
1 4 6 2 2 1 3 2 2 3 5 4 1
201
Teyberrs is a paradise for birds to live in. Assume you are a bird in Teyberrs, you are now flying somewhere like the game "Flappy Bird". You start flying at $$$(0,s)$$$, and every time when you are at $$$(x-1,y)$$$ ($$$1\leq x\leq n$$$), you must fly to either $$$(x,y-1)$$$ with cost $$$a_x$$$ or $$$(x,y+1)$$$ with cost $$$b_x$$$. Like the map in "Flappy Bird", you can not hit obstacles at $$$(x,y)$$$ where $$$y \lt l_x$$$ or $$$y \gt r_x$$$.
You will be given $$$q$$$ queries. In each query, you will be given two integers $$$x$$$ and $$$y$$$. Assume your target is at $$$(x,y)$$$, can you find the path with the minimum cost, or determine it is impossible?
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 200$$$), the number of test cases. For each test case:
The first line of the input contains three integers $$$n$$$, $$$q$$$ and $$$s$$$ ($$$1 \leq n,q \leq 200\,000$$$, $$$1\leq s\leq n$$$), denoting the size of the map, the number of queries, and the start point.
In the next $$$n$$$ lines, the $$$i$$$-th line contains four integers $$$a_i$$$, $$$b_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$1\leq a_i,b_i\leq 10^9$$$, $$$1\leq l_i\leq r_i\leq n$$$).
In the next $$$q$$$ lines, the $$$i$$$-th line contains two integers $$$x$$$ and $$$y$$$ ($$$1\leq x,y\leq n$$$), describing a target point.
It is guaranteed that the sum of all $$$n$$$ is at most $$$1\,000\,000$$$, and the sum of all $$$q$$$ is at most $$$1\,000\,000$$$.
For each query, print a single line containing an integer, denoting the minimum total cost. When it is impossible to reach the target, please print "-1" instead.
1 3 9 2 1 2 1 3 3 1 2 3 4 3 1 2 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
1 -1 2 -1 2 -1 6 -1 -1