M. Keystroke
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output
Examples
Input
2
2 1
0 1
0
1 2
1
0 1
Output
1
1
Input
2
2 2
0 1
0 1
1 1
1
1
Output
7
1