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<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);}
	double dot(Vec v){return add(x*v.x,y*v.y);}
	double det(Vec v){return add(x*v.y,-y*v.x);}
}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 sqrt(a.x*a.x+a.y*a.y);
}

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

int chC(Vec a,Cer c)
{
	return (a.x-c.cp.x)*(a.x-c.cp.x)+(a.y-c.cp.y)*(a.y-c.cp.y)<c.r*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(ch(t,s,W[j])<=W[j].r&&!(chC(s,W[j])&&chC(t,W[j])))f=1;
			puts(f?"Safe":"Danger");
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef double Real;
typedef complex<Real> P;

int n;
pair<P,Real> walls[125];

bool hit(pair<P,Real> wall, Real tx,Real ty,Real sx,Real sy){
    P o = wall.first;
    Real r = wall.second;
    P t = P(tx,ty);
    P s = P(sx,sy);
    // s to origin
    o -= s;
    t -= s;
    // t on x axis
    if(s==t)return false;
    o = (o / t) * abs(t);
    
    Real d = r*r - o.imag()*o.imag();
    if(d < 0.0)return false;
    Real x0 = o.real() + sqrt(d);
    Real x1 = o.real() - sqrt(d);
    Real tb = abs(t);
    if(0.0 <= x0 && x0 <= tb)return true;
    if(0.0 <= x1 && x1 <= tb)return true;
    return false;
}

int main(void){
    // Here your code !
    while(true){
        scanf("%d",&n);
        if(n==0)break;
        REP(i,n){
            Real x,y,r;
            scanf("%lf%lf%lf",&x,&y,&r);
            walls[i] = make_pair(P(x,y),r);
        }
        int m;
        scanf("%d",&m);
        while(m--){
            Real tx,ty,sx,sy;
            scanf("%lf%lf%lf%lf",&tx,&ty,&sx,&sy);
            bool ishit = false;
            REP(i,n)if(hit(walls[i],tx,ty,sx,sy)){
                ishit = true;
                break;
            }
            puts(ishit?"Safe":"Danger");
        }
    }
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int n,m;
vector<P> vec;
vector<int> rad;
P s,t;
ll sq(ll x)
{
	return x*x;
}
bool in(int x,P a)
{
	return sq(a.fi-vec[x].fi)+sq(a.sec-vec[x].sec)<rad[x]*rad[x];
}
bool check()
{
	for(int i=0;i<n;i++)
	{
		bool in1,in2;
		in1=in(i,s);in2=in(i,t);
		if(in1!=in2)return false;
		if(in1&&in2)continue;
		ll a=s.fi,b=s.sec,c=t.fi,d=t.sec,p=vec[i].fi,q=vec[i].sec;
		if(a>c)
		{
			swap(a,c);swap(c,d);
		}
		ll A=sq(d-b)+sq(c-a);
		ll B=(b*c-a*d)*(d-b)-p*sq(c-a)-(c-a)*(d-b)*q;
		ll C=p*p*sq(c-a)+sq(b*c-a*d-q*c+q*a)-sq(rad[i])*sq(c-a);
		//cout  << "At circle No." << i+1 << ' '<< A << ' ' << B << ' ' << C  << ' ' << B*B-A*C<< endl;
		if(B*B-A*C<0)continue;
		else if(B*B-A*C==0)
		{
			if(a<=(-B/A)&&(-B/A)<=c)return false;
			else continue;
		}
		else
		{
			double ans1=(-B+sqrt(B*B-A*C))/A;
			double ans2=(-B-sqrt(B*B-A*C))/A;
			if(ans1>=a&&ans2<=c)return false;
			else continue;
		}
	}
	return true;
}
int main()
{
	while(1)
	{
		vec.clear();
		rad.clear();
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++)
		{
			P k;
			int r;
			scanf("%d %d %d",&k.fi,&k.sec,&r);
			vec.pb(k);
			rad.pb(r);
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d %d %d %d",&t.fi,&t.sec,&s.fi,&s.sec);
			if(check())puts("Danger");
			else puts("Safe");
		}
	}
	return 0;
}