Closer Reviews Count More

You maintain a map of user reviews. Each review has a location and a score. When you want to estimate the score at a new location, nearby reviews should influence the estimate more than far ones. Use the k closest known reviews and weight each by w = 1/d, where d is the straight-line (Euclidean, L2) distance to the query point. If any known review is exactly at the query location (d = 0), ignore everyone else and predict the simple average of the scores of those exact matches.

The first line of the input contains a single integer n representing the number of known reviews. The next n lines contain two floating-point numbers x y followed by a floating-point score s, describing one review’s position and score.

The next line contains a single integer k representing how many nearby reviews to use.

The next line contains a single integer q representing the number of locations to estimate.
The last q lines each contain two floating-point numbers x y for the location to estimate.

The program should print the predicted scores.

Input

Output

3
0 0 4
2 0 2
0 2 5
2
2
1 0
0.5 1.5

3
4.690983005625052

4
0 0 2
1 1 3
1 1 4
2 2 5
3
2
1 1
1.1 1.1

3.5
3.5789473684210527

5
-1 0 1
2 0 2
0 3 4
1 1 5
3 3 3
2
3
1 0
0 2
10 10

3.5
4.414213562373095
3.447818343361264

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue