Problem: One of the questions children often ask is "How many stars are there in the sky?" Under ideal conditions, even with the naked eye, nearly eight thousands are observable in the northern hemisphere. With a decent telescope, you may find many more, but, as the sight field will be limited, you may find much less at a time.

Children may ask the same questions to their parents on a planet of some solar system billions of light-years away from the Earth. Their telescopes are similar to ours with circular sight fields, but alien kids have many eyes and can look into different directions at a time through many telescopes.

Given a set of positions of stars, a set of telescopes and the directions they are looking to, your task is to count up how many stars can be seen through these telescopes.



Input

The input consists of one or more datasets. The number of datasets is less than 50. Each dataset describes stars and the parameters of the telescopes used.

The first line of a dataset contains a positive integer n not exceeding 500, meaning the number of stars. Each of the n lines following it contains three decimal fractions, sx, sy, and sz. They give the position (sx, sy, sz) of the star described in Euclidean coordinates. You may assume -1000 ≤ sx ≤ 1000, -1000 ≤ sy ≤ 1000, -1000 ≤ sz ≤ 1000 and (sx, sy, sz) ≠ (0, 0, 0).

Then comes a line containing a positive integer m not exceeding 50, meaning the number of telescopes. Each of the following m lines contains four decimal fractions, tx, ty, tz, and φ, describing a telescope.

The first three numbers represent the direction of the telescope. All the telescopes are at the origin of the coordinate system (0, 0, 0) (we ignore the size of the planet). The three numbers give the point (tx, ty, tz) which can be seen in the center of the sight through the telescope. You may assume -1000 ≤ tx ≤ 1000, -1000 ≤ ty ≤ 1000, -1000 ≤ tz ≤ 1000 and (tx, ty, tz) ≠ (0, 0, 0).

The fourth number φ (0 ≤ φ ≤ π/2) gives the angular radius, in radians, of the sight field of the telescope.

Let us defie that θi,j is the angle between the direction of the i-th star and the center direction of the j-th telescope and φjis the angular radius of the sight field of the j-th telescope. The i-th star is observable through the j-th telescope if and only if θi,j is less than . You may assume that |θi,j - φj| > 0.00000001 for all pairs of i and j.

<image>


Figure 1: Direction and angular radius of a telescope

The end of the input is indicated with a line containing a single zero.

Output

For each dataset, one line containing an integer meaning the number of stars observable through the telescopes should be output. No other characters should be contained in the output. Note that stars that can be seen through more than one telescope should not be counted twice or more.

Example

Input

3
100 0 500
-500.243 -200.1 -300.5
0 300 200
2
1 1 1 0.65
-1 0 0 1.57
3
1 0 0
0 1 0
0 0 1
4
1 -1 -1 0.9553
-1 1 -1 0.9554
-1 -1 1 0.9553
-1 1 -1 0.9554
3
1 0 0
0 1 0
0 0 1
4
1 -1 -1 0.9553
-1 1 -1 0.9553
-1 -1 1 0.9553
-1 1 -1 0.9553
0


Output

2
1
0

Solution (in JAVA):

Solution:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	
	public static final int N_MAX = 500;
	public static final int M_MAX = 50;
	
	public static double inner_product(double[] in1, double[] in2){
		double sum = 0;
		
		for(int i = 0; i < in1.length; i++){
			sum += in1[i] * in2[i];
		}
		
		return sum;
	}
	
	public static double dist(double[] in){
		double ret = 0;
		for(double elem : in){
			ret += elem * elem;
		}
		
		return Math.sqrt(ret);
	}
	
	public static boolean check(double[] tele, double t_dist, double[] star, double rad){
		double[] small = new double[3];
		
		for(int i = 0; i < 3; i++){
			small[i] = tele[i] / t_dist;
		}
		
		double s_dist = 0;
		for(int i = 0; i < 3; i++){
			s_dist += (star[i] - small[i]) * (star[i] - small[i]);
		}
		s_dist = Math.sqrt(s_dist);
		
		double l_dist = Math.tan(t_dist);
		
		return l_dist >= s_dist;
	}
	
	public static void main(String[] args) {
		final Scanner sc = new Scanner(System.in);
		
		double[][] stars = new double[N_MAX][3];
		double[][] teles = new double[M_MAX][3];
		double[] rads = new double[M_MAX];
		boolean[] counted = new boolean[N_MAX];
		
		while(true){
			final int n = sc.nextInt();
			
			if(n == 0){
				break;
			}
			
			Arrays.fill(counted, false);
			
			for(int i = 0; i < n; i++){
				for(int j = 0; j < 3; j++){
					stars[i][j] = sc.nextDouble();
				}
			}
			
			final int m = sc.nextInt();
			
			for(int i = 0; i < m; i++){
				for(int j = 0; j < 3; j++){
					teles[i][j] = sc.nextDouble();
				}
				
				rads[i] = sc.nextDouble();
			}
			
			int count = 0;
			for(int tele = 0; tele < m; tele++){
				final double t_len = dist(teles[tele]);
				
				for(int star = 0; star < n; star++){
					if(counted[star]){
						continue;
					}
					
					final double s_len = dist(stars[star]);
					
					final double inner = inner_product(teles[tele], stars[star]);
					
					final double rad = Math.abs(Math.acos(inner / (s_len * t_len)));
					
					//System.out.println(rad + " " + rads[tele]);
					
					if(rads[tele] > rad){
						counted[star] = true;
						count++;
					}
					
					//double l_dist = inner / s_len;
					
					//System.out.println(t_len + " " + l_dist);
					
					//if(0 > l_dist){
					//	continue;
					//}
					
					//if(check(teles[tele], l_dist, stars[star], rads[tele])){
					//	counted[star] = true;
					//	count++;
					//}
				}
			}
			
			System.out.println(count);
		}
	}

}