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<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;
}

C++: #include<stdio.h>
#include<cmath>
#include<algorithm>
using namespace std;
const double EPS=1e-10;

double add(double a,double b)
{
	//if(abs(a+b)<EPS*(abs(a)+abs(b)))return 0;
	return a+b;
}

typedef struct Vec{
	double x,y;
	Vec(){}
	Vec(double x,double y):x(x),y(y){}
	Vec operator +(Vec v){return Vec(add(x,v.x),add(y,v.y));}
	Vec operator -(Vec v){return Vec(add(x,-v.x),add(y,-v.y));}
	Vec operator *(double d){return Vec(x*d,y*d);}
}Vec;

double dot(Vec a,Vec b){return add(a.x*b.x,a.y*b.y);}
double det(Vec a,Vec b){return add(a.x*b.y,-a.y*b.x);}

typedef struct Cer{
	double r;
	Vec cp;
}Cer;

double vabs(Vec a)
{
	return hypot(a.x,a.y);
}

double ch(Vec a,Vec b,Cer c)
{
	if(dot(b-a,c.cp-a)<EPS)return vabs(c.cp-a);
	if(dot(a-b,c.cp-b)<EPS)return vabs(c.cp-b);
	return fabs(det(b-a,c.cp-a))/vabs(b-a);
}

int chC(Vec a,Cer c)
{
	return hypot(a.x-c.cp.x,a.y-c.cp.y)<=c.r?1:0;
}

int N,M,i,j,f;
double m,d;
Cer W[100];

int main()
{
	for(;scanf("%d",&N),N;)
	{
		for(i=0;i<N;i++)
			scanf("%lf%lf%lf",&W[i].cp.x,&W[i].cp.y,&W[i].r);
		scanf("%d",&M);
		for(i=0;i<M;i++)
		{
			f=0;
			Vec t,s;
			scanf("%lf%lf%lf%lf",&t.x,&t.y,&s.x,&s.y);
			for(j=0;j<N;j++)
			{
				if(chC(s,W[j])&&chC(t,W[j]))continue;
				if(ch(t,s,W[j])<=W[j].r)
					f=1;
			}
			puts(f?"Safe":"Danger");
		}
	}
	return 0;
}

C++: // 2011/03/04 Tazoe

#include <iostream>
using namespace std;

struct wall{
	int x;
	int y;
	int r;
};

bool saf_jdg(struct wall wal, int x1, int y1, int x2, int y2)
{
	if((x1-wal.x)*(x1-wal.x)+(y1-wal.y)*(y1-wal.y)<wal.r*wal.r
	 &&(x2-wal.x)*(x2-wal.x)+(y2-wal.y)*(y2-wal.y)<wal.r*wal.r)
		return false;

	int vx = x2-x1;
	int vy = y2-y1;
	int cx = wal.x-x1;
	int cy = wal.y-y1;

	int vc = vx*cx+vy*cy;

	int v2 = vx*vx+vy*vy;
	int c2 = cx*cx+cy*cy;
	int r2 = wal.r*wal.r;

	if(vc<0)
		if(c2<r2)
			return true;
		else
			return false;

	if(vc>v2){
		int dx = wal.x-x2;
		int dy = wal.y-y2;
		int d2 = dx*dx+dy*dy;
		if(d2<r2)
			return true;
		else
			return false;
	}

	if(c2*v2-vc*vc<=r2*v2)
		return true;
	else
		return false;
}

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		struct wall wal[100];
		for(int i=0; i<n; i++)
			cin >> wal[i].x >> wal[i].y >> wal[i].r;

		int m;
		cin >> m;

		for(int i=0; i<m; i++){
			int tx, ty, sx, sy;
			cin >> tx >> ty >> sx >> sy;

			bool saf_flg = false;
			for(int j=0; j<n; j++)
				if(saf_jdg(wal[j], tx, ty, sx, sy)){
					saf_flg = true;
					break;
				}

			if(saf_flg)
				cout << "Safe" << endl;
			else
				cout << "Danger" << endl;
		}
	}

	return 0;
}