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<math.h>
struct P{double x,y;};
struct W{P p;double r;}w[100];
double D(P p){return sqrt(p.x*p.x+p.y*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 <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <bitset>
#include <stack>
#include <cstdlib>
#include <complex>
#include <unordered_map>
#define REP(i,n) for(int i = 0; n > i; i++)
#define MODU 33
#define Range(x,a,b) ((a) <= (x) && (x) <= (b))
#define POWT(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef vector<int> Ivec;
typedef pair<int, int> pii;
typedef long long int ll;

////???????????¬////
const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> Pt;//???
namespace std {
	bool operator < (const Pt& a, const Pt& b) {
		return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
	}
}
double cross(const Pt& a, const Pt& b) {
	return imag(conj(a)*b);
}
double dot(const Pt& a, const Pt& b) {
	return real(conj(a)*b);
}

struct Ln : public vector<Pt> {//???
	Ln(const Pt &a, const Pt &b) {
		push_back(a); push_back(b);
	}
};

typedef vector<Pt> Pol;//????§???¢

struct Cir {//???
	Pt p; double r;
	Cir(const Pt &p, double r) : p(p), r(r) { }
};

int ccw(Pt a, Pt b, Pt c) {
	b -= a; c -= a;
	if (cross(b, c) > 0)   return +1;       // counter clockwise
	if (cross(b, c) < 0)   return -1;       // clockwise
	if (dot(b, c) < 0)     return +2;       // c--a--b on line
	if (norm(b) < norm(c)) return -2;       // a--b--c on line
	return 0;
}

Pt projection(const Ln &l, const Pt &p) {
	double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
	return l[0] + t*(l[0] - l[1]);
}
bool intersectSP(const Ln &s, const Pt &p) {//????????????
	return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS; // triangle inequality
}
double distanceSP(const Ln &s, const Pt &p) {
	const Pt r = projection(s, p);
	if (intersectSP(s, r)) return abs(r - p);
	return min(abs(s[0] - p), abs(s[1] - p));
}

bool intersectCL(const Ln &s, const Cir &c) {
	double dis = distanceSP(s, c.p);
	return dis <= c.r;
}
double distancePP(const Pt &p, const Pt &q) {
	return hypot(p.real() - q.real(), p.imag() - q.imag());
}
bool insideCir(Pt &p, Cir &c) {
	return distancePP(p, c.p) <= c.r;
}
int main() {
	int n, m;
	vector<Cir> walls;
	scanf("%d", &n);
	REP(i,n){
		double px, py, r;
		scanf("%lf %lf %lf", &px, &py, &r);
		walls.push_back({ {px,py},r });
	}

	scanf("%d", &m);
	REP(i, m) {
		Pt t, s;
		double x, y;
		scanf("%lf %lf", &x, &y);
		t = { x,y };
		scanf("%lf %lf", &x, &y);
		s = { x,y };

		bool f = 0;
		REP(j, n) {
			f = intersectCL({ t,s }, walls[j]);
			if (f) {
				if (insideCir(t, walls[j]) && insideCir(s, walls[j]))
					f = 0;
				else
					break;
			}
		}
		if (f) {
			printf("Safe\n");
		}
		else 
			printf("Danger\n");
	}

	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <utility>
#include <vector>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
typedef pair<int,int> p;
int tx,ty,sx,sy;
double r,rx,ry;
bool cir(double x,double y){
	return (x-rx)*(x-rx)+(y-ry)*(y-ry)<r*r;
}
int main()
{
    int n,m;
	while(cin>>n&&n){
		pair<int,p> wall[100];//<r,<x,y>>
		for(int i=0;i<n;i++)cin>>wall[i].second.first>>wall[i].second.second>>wall[i].first;
		cin>>m;
		for(int i=0;i<m;i++){
			string ans="Danger";
			cin>>tx>>ty>>sx>>sy;
			double a,b,c;
			if(tx!=sx&&ty!=sy){
				a=(double)(ty-sy)/(tx-sx);
				b=(double)-1;
				c=ty-(double)a*tx;
			}else if(tx==sx){
				a=1;
				b=0;
				c=-tx;
			}else if(ty==sy){
				a=0;
				b=1;
				c=-ty;
			}
			double bo=sqrt(a*a+b*b);
			double d=(a*a+b*b)/(a*b);
			for(int j=0;j<n;j++){
				r=(double)wall[j].first;
				rx=(double)wall[j].second.first;
				ry=(double)wall[j].second.second;
				double shi=abs(a*rx+b*ry+c);
				double res=shi/bo;
				//printf("a:%.3f b:%.3f c:%.3f shi:%.3f bo:%.3f res:%.3f\n",a,b,c,shi,bo,res);
				if(r<res)continue;
				double px,py;//?????????????????????????????¨?????????
				if((a!=0)&&(b!=0)){
					px=(b/a*rx-ry-c/b)/d;
					py=-a/b*px-c/b;
				}else if(a!=0){//y????????????
					px=-c;
					py=ry;
				}else if(b!=0){//x????????????
					py=-c;
					px=rx;
				}
				if(cir((double)sx,(double)sy)&&cir((double)tx,(double)ty)){
					continue;
				}else if(cir((double)sx,(double)sy)!=cir((double)tx,(double)ty)||
					((double)min(sx,tx)<px&&px<(double)max(sx,tx))||((double)min(sy,ty)<py&&py<(double)max(sy,ty))){
					ans="Safe";
					break;
				}
			}
			cout<<ans<<endl;
		}
	}
    return 0;
}