Inner World
题号:NC51641
时间限制:C/C++/Rust/Pascal 3秒,其他语言6秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

Gromah and LZR are transfered to a forest, maybe it is the inner world of the great tomb. Initially, there are  rooted trees numbered from  to  with size  in the forest. For each tree, the only node is the root and labeled with .

After a while, here comes a farmer, and the farmer gives them  planting tasks, each can be described by a tuple , which means to add a labeled node  for all trees numbered from to , and their parent nodes are the nodes labeled with  for each tree.

After finishing the planting tasks one by one, the farmer will give them  querying tasks, each can be described by a tuple , which means to query the sum of sizes of subtrees whose roots are the nodes labeled with  among the trees numbered from  to . Specially, if there isn't a node labeled with  in a tree, the size of subtree  is regarded as .

If they complete all tasks perfectly, the farmer will help them pass the final level.

Please help them handle these tasks.

输入描述:

The first line contains two positive integers , denoting the number of trees in the inner world and the number of planting tasks.

Following  lines each contains four positive integers , denoting a planting task .

The next line contains one positive integer , denoting the number of querying tasks.

Following  lines each contains four positive integers , denoting a querying task .



For each planting task, It is guaranteed that the label  is unique among all s, and the trees numbered from  to  all have a node labeled with  right before handling this task.

输出描述:

Output  lines, each contains one non-negative integer denoting the answer to corresponding query task.
示例1

输入

复制
4 3
1 2 1 2
1 3 2 4
3 4 2 3
2
1 1 4
3 1 4

输出

复制
11
5

说明

Four trees are 1-2, 1(-2)-3-4, 1-3-4, 1-3.

In the four trees, sizes of subtrees 1 are 2,4,3,2, so the answer to the first query task is 2+4+3+2=11, while the sizes of subtrees 3 are 0,2,2,1 and the answer to the second query task is 0+2+2+1=5.