Excited Database

She says that any Pavarotti among the nightingales will serenade his mate while she sits on her eggs.
She says that any excited database can answer the queries efficiently.

You are given the two dimensional database as a matrix A with n rows and n columns. In the beginning, A[i][j]=0 for all 1≤i,j≤n.
Then q operations or queries will be given in turn.

You should maintain the database for two type of operations:⋅1LR: for each element A[i][j] which satisfy L≤i+j≤R, increase the value to A[i][j]+1, where 2≤L≤R≤2n.⋅2LR: for each element A[i][j] which satisfy L≤i−j≤R, increase the value to A[i][j]+1, where 1−n≤L≤R≤n−1.
Meanwhile, you should answer the queries:⋅3x1x2y1y2: count the value of elements A[i][j] which satisfy x1≤i≤x2 and y1≤j≤y2, where 1≤x1<x2≤n and 1≤y1<y2≤n.

Input

The input contains several test cases. The first line of the input is a single integer t which is the number of test cases. Then t test cases follow.

Each test case contains several lines. The first line contains the integer n and q.
The i-th line of the next q lines contains an operation ‘‘1LR" or ‘‘2LR", or a query ‘‘3x1x2y1y2".

The sum of n for all test cases would not be larger than 200000 and the sum of q would not be larger than 50000.

Output

For each test case, you should output answers to queries printed one per line.

Ruritania is a country with a very badly maintained road network, which is not exactly good news for lorry drivers that constantly have to do deliveries. In fact, when roads are maintained, they become one-way. It turns out that it is sometimes impossible to get from one town to another in a legal way – however, we know that all towns are reachable, though illegally!

Fortunately for us, the police tend to be very corrupt and they will allow a lorry driver to break the rules and drive in the wrong direction provided they receive ‘a small gift’. There is one patrol car for every road and they will request 1000 Ruritanian dinars when a driver drives in the wrong direction. However, being greedy, every time a patrol car notices the same driver breaking the rule, they will charge double the amount of money they requested the previous time on that particular road.

Borna is a lorry driver that managed to figure out this bribing pattern. As part of his job, he has to make K stops in some towns all over Ruritania and he has to make these stops in a certain order. There are N towns (enumerated from 1 to N) in Ruritania and Borna’s initial location is the capital city i.e. town 1. He happens to know which ones out of the N - 1 roads in Ruritania are currently unidirectional, but he is unable to compute the least amount of money he needs to prepare for bribing the police. Help Borna by providing him with an answer and you will be richly rewarded.

Input

The first line contains N, the number of towns in Ruritania. The following N - 1 lines contain information regarding individual roads between towns. A road is represented by a tuple of integers (a,b,x), which are separated with a single whitespace character. The numbers a and b represent the cities connected by this particular road, and x is either 0 or 1: 0 means that the road is bidirectional, 1 means that only the a → b direction is legal. The next line contains K, the number of stops Borna has to make. The final line of input contains K positive integers s1, …, sK: the towns Borna has to visit.

1 ≤ N ≤ 105

1 ≤ K ≤ 106

1 ≤ a, b ≤ N for all roads

for all roads

1 ≤ si ≤ N for all 1 ≤ i ≤ K

Output

The output should contain a single number: the least amount of thousands of Ruritanian dinars Borna should allocate for bribes, modulo 109 + 7.

Sample test(s)

Input

C++

1

2

3

4

5

6

7

5

120

230

511

341

5

54522

Output

C++

1

4

Note

Borna first takes the route 1 → 5 and has to pay 1000 dinars. After that, he takes the route 5 → 1 → 2 → 3 → 4 and pays nothing this time. However, when he has to return via 4 → 3 → 2 → 1 → 5, he needs to prepare 3000 (1000+2000) dinars. Afterwards, getting to 2 via 5 → 1 → 2 will cost him nothing. Finally, he doesn't even have to leave town 2 to get to 2, so there is no need to prepare any additional bribe money. Hence he has to prepare 4000 dinars in total.

tree

Given a rooted tree(node 1 is the root) with n nodes. The ithnode has a positive value vi at beginning.
We define the universal set S includes all nodes.
There are two types of Memphis's operation.
First, Memphis may change the value of one node. It's the first type operation:

This problem has multi test cases(no more than 3).
The first line contains a single integer T, which denotes the number of test cases.
For each test case,the first line contains two non-negative integer n,m(1≤n,m≤100000) - the number of nodes and operations.
The second line contains n−1 non-negative integer f2∼fn(fi<i) - the father of ithnode.
The third line contains n non-negative integer v1∼vn(0≤vi≤109) - the value of nodes at beginning.
Follow m lines describe each operation.

Output

For each test cases,for each second operation print a non-negative integer.

The LCIS on the Tree

For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i <= j <= N and Si < Si+1 < Si+2 < ... < Sj-1 < Sj , then the sequence Si, Si+1, ... , Sj is a CIS(Continuous Increasing Subsequence). The longest CIS of a sequence is called the LCIS (Longest Continuous Increasing Subsequence).
Now we consider a tree rooted at node 1. Nodes have values. We have Q queries, each with two nodes u and v. You have to find the shortest path from u to v. And write down each nodes' value on the path, from u to v, inclusive. Then you will get a sequence, and please show us the length of its LCIS.

Input

The first line has a number T (T <= 10) , indicating the number of test cases.
For each test case, the first line is a number N (N <= 105), the number of nodes in the tree.
The second line comes with N numbers v1, v2, v3 ... , vN, describing the value of node 1 to node N. (1 <= vi <= 109)
The third line comes with N - 1 numbers p2, p3, p4 ... , pN, describing the father nodes of node 2 to node N. Node 1 is the root and will have no father.
Then comes a number Q, it is the number of queries. (Q <= 105)
For next Q lines, each with two numbers u and v. As described above.

Output

For test case X, output "Case #X:" at the first line.
Then output Q lines, each with an answer to the query.
There should be a blank line *BETWEEN* each test case.

XYZ and Drops

XYZ is playing an interesting game called "drops". It is played on a r∗c grid. Each grid cell is either empty, or occupied by a waterdrop. Each waterdrop has a property "size". The waterdrop cracks when its size is larger than 4, and produces 4 small drops moving towards 4 different directions (up, down, left and right).

In every second, every small drop moves to the next cell of its direction. It is possible that multiple small drops can be at same cell, and they won't collide. Then for each cell occupied by a waterdrop, the waterdrop's size increases by the number of the small drops in this cell, and these small drops disappears.

You are given a game and a position (x, y), before the first second there is a waterdrop cracking at position (x, y). XYZ wants to know each waterdrop's status afterT seconds, can you help him?

1≤r≤100, 1≤c≤100, 1≤n≤100, 1≤T≤10000

Input

The first line contains four integers r, c, n and T. n stands for the numbers of waterdrops at the beginning.
Each line of the following n lines contains three integers xi, yi, sizei, meaning that the i-th waterdrop is at position (xi, yi) and its size is sizei. (1≤sizei≤4)
The next line contains two integers x, y.

n lines. Each line contains two integers Ai, Bi:
If the i-th waterdrop cracks in T seconds, Ai=0, Bi= the time when it cracked.
If the i-th waterdrop doesn't crack in T seconds, Ai=1, Bi= its size after T seconds.

Boring Class

Mr. Zstu and Mr. Hdu are taking a boring class , Mr. Zstu comes up with a problem to kill time, Mr. Hdu thinks it’s too easy, he solved it very quickly, what about you guys?
Here is the problem:
Give you two sequences L1,L2,...,Ln and R1,R2,...,Rn.
Your task is to find a longest subsequence v1,v2,...vm satisfiesv1≥1,vm≤n,vi<vi+1 .(for i from 1 to m - 1)Lvi≥Lvi+1,Rvi≤Rvi+1(for i from 1 to m - 1)
If there are many longest subsequence satisfy the condition, output the sequence which has the smallest lexicographic order.

Input

There are several test cases, each test case begins with an integer n.1≤n≤50000
Both of the following two lines contain n integers describe the two sequences.1≤Li,Ri≤109

Output

For each test case ,output the an integer m indicates the length of the longest subsequence as described.
Output m integers in the next line.

Walk Out

In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit). In every position of this maze, there is either a 0 or a 1 written on it.

An explorer gets lost in this grid. His position now is (1,1), and he wants to go to the exit. Since to arrive at the exit is easy for him, he wants to do something more difficult. At first, he'll write down the number on position (1,1). Every time, he could make a move to one adjacent position (two positions are adjacent if and only if they share an edge). While walking, he will write down the number on the position he's on to the end of his number. When finished, he will get a binary number. Please determine the minimum value of this number in binary system.

Input

The first line of the input is a single integer T(T=10), indicating the number of testcases.

For each testcase, the first line contains two integers n and m(1≤n,m≤1000). The i-th line of the next n lines contains one 01 string of length m, which represents i-th row of the maze.

Output

For each testcase, print the answer in binary system. Please eliminate all the preceding 0 unless the answer itself is 0 (in this case, print 0 instead).

Solve this interesting problem

Have you learned something about segment tree? If not, don’t worry, I will explain it for you.
Segment Tree is a kind of binary tree, it can be defined as this:
- For each node u in Segment Tree, u has two values: Lu and Ru.
- If Lu=Ru, u is a leaf node.
- If Lu≠Ru, u has two children x and y,with Lx=Lu,Rx=⌊Lu+Ru2⌋,Ly=⌊Lu+Ru2⌋+1,Ry=Ru.
Here is an example of segment tree to do range query of sum.

Given two integers L and R, Your task is to find the minimum non-negative n satisfy that: A Segment Tree with root node's value Lroot=0 and Rroot=n contains a node u with Lu=L and Ru=R.

Input

The input consists of several test cases.
Each test case contains two integers L and R, as described above.0≤L≤R≤109LR−L+1≤2015

Output

For each test, output one line contains one integer. If there is no such n, just output -1.

RGCDQ

Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more interesting things about GCD. Today He comes up with Range Greatest Common Divisor Query (RGCDQ). What’s RGCDQ? Please let me explain it to you gradually. For a positive integer x, F(x) indicates the number of kind of prime factor of x. For example F(2)=1. F(10)=2, because 10=2*5. F(12)=2, because 12=2*2*3, there are two kinds of prime factor. For each query, we will get an interval [L, R], Hdu wants to know maxGCD(F(i),F(j))(L≤i<j≤R)

Input

There are multiple queries. In the first line of the input file there is an integer T indicates the number of queries.
In the next T lines, each line contains L, R which is mentioned above.All input items are integers.
1<= T <= 1000000
2<=L < R<=1000000

Output

For each query，output the answer in a single line.
See the sample for more details.

Magician

Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an innate talent, gaining it through study and practice, or receiving it from another being, often a god, spirit, or demon of some sort. Some wizards are depicted as having a special gift which sets them apart from the vast majority of characters in fantasy worlds who are unable to learn magic.

Magicians, sorcerers, wizards, magi, and practitioners of magic by other titles have appeared in myths, folktales, and literature throughout recorded history, with fantasy works drawing from this background.

In medieval chivalric romance, the wizard often appears as a wise old man and acts as a mentor, with Merlin from the King Arthur stories representing a prime example. Other magicians can appear as villains, hostile to the hero.

Mr. Zstu is a magician, he has many elves like dobby, each of which has a magic power (maybe negative). One day, Mr. Zstu want to test his ability of doing some magic. He made the elves stand in a straight line, from position 1 to position n, and he used two kinds of magic, Change magic and Query Magic, the first is to change an elf’s power, the second is get the maximum sum of beautiful subsequence of a given interval. A beautiful subsequence is a subsequence that all the adjacent pairs of elves in the sequence have a different parity of position. Can you do the same thing as Mr. Zstu ?

Input

The first line is an integer T represent the number of test cases.
Each of the test case begins with two integers n, m represent the number of elves and the number of time that Mr. Zstu used his magic.
(n,m <= 100000)
The next line has n integers represent elves’ magic power, magic power is between -1000000000 and 1000000000.
Followed m lines, each line has three integers like
type a b describe a magic.
If type equals 0, you should output the maximum sum of beautiful subsequence of interval [a,b].(1 <= a <= b <= n)
If type equals 1, you should change the magic power of the elf at position a to b.(1 <= a <= n, 1 <= b <= 1e9)