Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	x := make([]int, 100)
	y := make([]int, 100)
	r := make([]int, 100)
	for scanner.Scan() {
		a, _ := strconv.Atoi(scanner.Text())
		if a == 0 {
			break
		}
		for i := 0; i < a; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			tmpx, _ := strconv.Atoi(input[0])
			tmpy, _ := strconv.Atoi(input[1])
			tmpr, _ := strconv.Atoi(input[2])
			x[i], y[i], r[i] = tmpx, tmpy, tmpr
		}
		scanner.Scan()
		b, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < b; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			c, _ := strconv.Atoi(input[0])
			d, _ := strconv.Atoi(input[1])
			e, _ := strconv.Atoi(input[2])
			f, _ := strconv.Atoi(input[3])
			ok := false
			for j := 0; j < a; j++ {
				if (c-x[j])*(c-x[j])+(d-y[j])*(d-y[j]) >= r[j]*r[j] || (e-x[j])*(e-x[j])+(f-y[j])*(f-y[j]) >= r[j]*r[j] {
					if (e-c)*(x[j]-c)+(f-d)*(y[j]-d) < 0 {
						if (x[j]-c)*(x[j]-c)+(y[j]-d)*(y[j]-d) <= r[j]*r[j] {
							ok = true
						}
					} else {
						if (e-c)*(x[j]-c)+(f-d)*(y[j]-d)>(e-c)*(e-c)+(f-d)*(f-d) {
							if (x[j]-e)*(x[j]-e)+(y[j]-f)*(y[j]-f)<=r[j]*r[j] {
								ok = true
							}
						} else {
							if (x[j]-c)*(x[j]-c)+(y[j]-d)*(y[j]-d)-((e-c)*(x[j]-c)+(f-d)*(y[j]-d))*((e-c)*(x[j]-c)+(f-d)*(y[j]-d))/((e-c)*(e-c)+(f-d)*(f-d))<=r[j]*r[j] {
								ok = true
							}
						}
					}

				}
			}
			if ok {
				fmt.Println("Safe")
			} else {
				fmt.Println("Danger")
			}
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;

int n, m, wall[100][3], pp[4];
bool jud;
int main(){
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> wall[i][0] >> wall[i][1] >> wall[i][2];
	}
	cin >> m;
	for(int i = 0; i < m; i++){
		for(int j = 0; j < 4; j++){
			cin >> pp[j];
		}
		double dx = pp[0] - pp[2], dy = pp[1] - pp[3], kmk, spn;
		if(dx)
			kmk = dy / dx;
		else{
			for(int j = 0; j < n; j++){
				double a = 1, b = -2 * wall[j][1], c = (pp[0] - wall[j][0]) * (pp[0] - wall[j][0]) + wall[j][1] * wall[j][1] - wall[j][2] * wall[j][2];
				if(b * b - 4 * a * c >= 0){
					double aa = (sqrt(b * b - 4 * a * c) - b) / (2 * a);
				//	cout << aa << endl;
					if(aa >= pp[1] && aa <= pp[3]){
						jud = 1;
						break;
					}
					if(aa >= pp[3] && aa <= pp[1]){
						jud = 1;
						break;
					}
					aa = (-sqrt(b * b - 4 * a * c) - b) / (2 * a);
				//	cout << aa << endl;
					if(aa >= pp[1] && aa <= pp[3]){
						jud = 1;
						break;
					}
					if(aa >= pp[3] && aa <= pp[1]){
						jud = 1;
						break;
					}
				} 
			}
			if(jud)
				cout << "Safe" << endl;
			else
				cout << "Danger" << endl;
			continue;
		}
		spn = -kmk * pp[0] + pp[1];
		jud = 0;
		for(int j = 0; j < n; j++){
			double a = 1 + kmk * kmk, b = -2 * wall[j][0] + 2 * kmk * spn - 2 * kmk * wall[j][1], c = wall[j][0] * wall[j][0] + wall[j][1] * wall[j][1] + spn * spn - 2 * spn * wall[j][1] - wall[j][2] * wall[j][2];
				if(b * b - 4 * a * c >= 0){
				double aa = (sqrt(b * b - 4 * a * c) - b) / (2 * a);
				if(aa >= pp[0] && aa <= pp[2]){
					jud = 1;
					break;
				}
				if(aa >= pp[2] && aa <= pp[0]){
					jud = 1;
						break;
				}
				aa = (-sqrt(b * b - 4 * a * c) - b) / (2 * a);
				if(aa >= pp[0] && aa <= pp[2]){
					jud = 1;
					break;
				}
				if(aa >= pp[2] && aa <= pp[0]){
					jud = 1;
					break;
				}
			}
		}
		if(jud)
			cout << "Safe" << endl;
		else
			cout << "Danger" << endl;
	}
}

C++: #include<stdio.h>
#include<math.h>
struct P{double x,y;};
struct W{P p;double r;}w[100];
double D(P p){return hypot(p.x,p.y);}
double I(P a,P b){return a.x*b.x+a.y*b.y;}
double O(P a,P b){return a.x*b.y-a.y*b.x;}
P F(P l,P r){P p={l.x-r.x,l.y-r.y};return p;}
double S(P a,P b,P c)
{
	if(I(F(b,a),F(c,a))<1e-9)return D(F(c,a));
	if(I(F(a,b),F(c,b))<1e-9)return D(F(c,b));
	return fabs(O(F(b,a),F(c,a)))/D(F(b,a));
}
int main()
{
	P a,b;
	int i,n,m;
	while(scanf("%d",&n),n)
	{
		for(i=0;i<n;++i)scanf("%lf%lf%lf",&w[i].p.x,&w[i].p.y,&w[i].r);
		for(scanf("%d",&m);m--;)
		{
			scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
			for(i=0;i<n;++i)
			{
				if(D(F(a,w[i].p))<=w[i].r&&D(F(b,w[i].p))<=w[i].r)continue;
				if(S(a,b,w[i].p)<=w[i].r){puts("Safe");goto E;}
			}
			puts("Danger");
E:;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<cmath>
#include<vector>

#define EPS 1e-5
#define PI 3.141592654

class Point{
	double x;
	double y;
public:
	Point( double a , double b ):x(a),y(b){}
	double X() const{ return x; }
	double Y() const{ return y; }
	double Norm() const{ return sqrt( pow( x , 2 ) + pow( y , 2 ) ); }
};

typedef Point Vector;

class Circle{
	Point o;
	double r;
public:
	Circle( double x , double y , double rad ):o(x,y),r(rad){}
	Point O(){ return o; }
	double R(){ return r; }
};

double getDistance(const Point& a, const Point& b)
{
	return sqrt( pow(a.X() - b.X() , 2) + pow(a.Y() - b.Y() , 2));
}

double getAreaOfTri( const Point& a , const Point& b , const Point& c )
{
	// ヘロンの公式
	double ab = getDistance( a , b );
	double bc = getDistance( b , c );
	double ca = getDistance( c , a );
	double s = ( ab + bc + ca ) / 2.0;
	double Area = sqrt( s * (s - ab) * (s - bc) * (s - ca) );
	return Area;
}

double getDistancePointLine( const Point& o , const Point& a , const Point& b )
{
	double area = getAreaOfTri( o , a , b );
	double ab = getDistance( a , b );
	double h = 2 * area / ab;
	return h;
}

Point getMiddlePoint( const Point& a , const Point& b )
{
	double middleX = (a.X() + b.X()) / 2.0;
	double middleY = (a.Y() + b.Y()) / 2.0;
	Point middle( middleX , middleY );
	return middle;
}

double getDotProduct( const Vector& a , const Vector& b )
{
	return a.X() * b.X() + a.Y() * b.Y();
}

bool isWall( const Point& t , const Point& s , Circle wall )
{
	Vector ts( s.X() - t.X() , s.Y() - t.Y() );
	Vector to( wall.O().X() - t.X() , wall.O().Y() - t.Y() );
	Vector st( t.X() - s.X() , t.Y() - s.Y() );
	Vector so( wall.O().X() - s.X() , wall.O().Y() - s.Y() );

	double angleOTS = acos( getDotProduct( ts , to ) / ( ts.Norm() * to.Norm() ) );
	double angleOST = acos( getDotProduct( st , so ) / ( st.Norm() * so.Norm() ) );

	// 壁になるときにtrueを返す。
	if( wall.R() > getDistance( t , wall.O() ) && wall.R() > getDistance( s , wall.O() ) ){
		return false;
	}
	else if( wall.R() < getDistance( t , wall.O() ) && wall.R() < getDistance( s , wall.O() ) ){
		/*
		if( getDistancePointLine( wall.O() , t , s ) > wall.R() ){
			return false;
		}
		*/
		/*
		if( getDistance( getMiddlePoint( t , s ) , wall.O() ) > wall.R() ){
			return false;
		}
		*/
		if( !(angleOTS <= PI/2 + EPS && angleOST <= PI/2 + EPS && getDistancePointLine( wall.O() , t , s ) <= wall.R() + EPS ) ){
			return false;
		}
	}
	else{
		return true;
	}
}

int main(void)
{
	int n, m;
	std::vector<Circle> wall;
	while(1){
		std::cin >> n;
		if( n == 0 ){
			break;
		}
		for( int i = 0 ; i < n ; i++ ){
			int x, y, r;
			std::cin >> x >> y >> r;
			Circle cir( x , y , r );
			wall.push_back(cir);
		}
		std::cin >> m;
		for( int i = 0 ; i < m ; i++ ){
			int tx, ty, sx, sy;
			std::cin >> tx >> ty >> sx >> sy;
			Point t(tx, ty);
			Point s(sx, sy);
			int flag = 0;
			for( int j = 0 ; j < n ; j++ ){
				if( isWall( t , s , wall[j] ) ){
					flag = 1;
					break;
				}
			}
			std::cout << (flag == 1 ? "Safe":"Danger") << std::endl;
		}
	}

	return 0;
}