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 <cstdio>
#include <cstdlib>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>

#define rep(x,to) for(int x=0;x<to;x++)
#define rep2(x,from,to) for(int x=from;x<to;x++)
#define SQ(x) ((x)* (x))

using namespace std;

// 円と線分の当たり判定 //
bool jES(int sx1, int sy1, int sx2, int sy2, int x, int y, int r) {
    int d2;
    int a,b, c;
	
	a = sy2 - sy1;
	b = sx1 - sx2;
	c = -1 * (a * sx1 + b * sy1);  
	// d = abs(a*x+b*y+c)/sqrt(a*a+b*b)
	d2 = SQ(a*x+b*y+c);
	if(d2 > r*r * (a*a + b*b)) return false;
	
	if(((sx2-sx1)*(x-sx1)+(sy2-sy1)*(y-sy1)>0) &&
	   ((sx1-sx2)*(x-sx2)+(sy1-sy2)*(y-sy2)>0))  return true;
	
	return false;
}


int main(void){
 
int n, m;
int w[110][3];
int tx,ty,sx,sy;
	
	while(cin >> n){
		if(n==0 || cin.eof()) break;
		rep(i,n) cin >> w[i][0] >> w[i][1]>> w[i][2];
		
		cin >> m;
		rep(i,m){
			cin >> tx >> ty >> sx >> sy;
			bool ok = true;
			rep(j,n){
				if((SQ(tx-w[j][0]) + SQ(ty-w[j][1]) < SQ(w[j][2])) &&
				(SQ(sx-w[j][0]) + SQ(sy-w[j][1]) < SQ(w[j][2]))) ok=false;
				else if ((SQ(tx-w[j][0]) + SQ(ty-w[j][1]) > SQ(w[j][2])) &&
				(SQ(sx-w[j][0]) + SQ(sy-w[j][1]) < SQ(w[j][2]))) ok=true;
				else if ((SQ(tx-w[j][0]) + SQ(ty-w[j][1]) < SQ(w[j][2])) &&
				(SQ(sx-w[j][0]) + SQ(sy-w[j][1]) > SQ(w[j][2]))) ok=true;
				else  ok = jES(tx,ty,sx,sy,w[j][0],w[j][1],w[j][2]);
		//		cout << "test:::" <<(ok?  "Safe": "Danger") << endl;
				if(ok) break;
			}
			cout << (ok? "Safe": "Danger") << endl;
		}
	}
}

C++: #include <iostream>
#include <complex>
#include <vector>
#include <algorithm>

#define EPS 1e-7

using namespace std;

typedef complex<double> P;

struct L : public vector<P> {
    L(const P &a, const P &b){
        push_back(a);
        push_back(b);
    }
};

struct C{
    P p;
    double r;
    C(const P &p, double r) : p(p), r(r) { }
};

double dot(const P &a, const P &b){
    return real(conj(a) * b);
}

bool intersectSP(const L &s, const P &p) {
    return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}

P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}

double distanceSP(const L &s, const P &p) {
  const P r = projection(s, p);
  if (intersectSP(s, r)) return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

int main(){
    int n,m;                                     
    double x,y,r;
    double a,b;
    vector<C> circles;
    while(cin >> n && n){
        bool isok = false;
        for(int i=0;i<n;i++){
            cin >> x >> y >> r;
            circles.push_back(C(P(x,y), r));
        }
        cin >> m;
        for(int i=0;i<m;i++){
            isok = false;
            cin >> x >> y;
            cin >> a >> b;
            L line = L(P(x,y), P(a,b));
            for(int j=0;j<n;j++){
                if((abs(line[0]-circles[j].p) < circles[j].r &&
                    abs(line[1]-circles[j].p) < circles[j].r) ||
                   distanceSP(line, circles[j].p) > circles[j].r){
                }else{
                    isok = true;
                }
            }
            if(isok){
                cout << "Safe" << endl;
            }else{
                cout << "Danger" << endl;
            }
        }
    }
}

C++: #include <iostream>
#include <complex>
#include <vector>
#include <algorithm>

#define EPS 1e-6

using namespace std;

typedef complex<double> P;

struct L : public vector<P> {
    L(const P &a, const P &b){
        push_back(a);
        push_back(b);
    }
};

struct C{
    P p;
    double r;
    C(const P &p, double r) : p(p), r(r) { }
};

double dot(const P &a, const P &b){
    return real(conj(a) * b);
}

bool intersectSP(const L &s, const P &p) {
    return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}

P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}

double distanceSP(const L &s, const P &p) {
  const P r = projection(s, p);
  if (intersectSP(s, r)) return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

int main(){
    int n,m;                                     
    double x,y,r;
    double a,b;
    vector<C> circles;
    while(cin >> n && n){
        for(int i=0;i<n;i++){
            cin >> x >> y >> r;
            circles.push_back(C(P(x,y), r));
        }
        cin >> m;
        for(int i=0;i<m;i++){
            bool isok = false;
            cin >> x >> y;
            cin >> a >> b;
            L line = L(P(x,y), P(a,b));
            for(int j=0;j<n;j++){
                if((abs(line[0]-circles[j].p) - circles[j].r < -EPS &&
                    abs(line[1]-circles[j].p) - circles[j].r < -EPS) ||
                   distanceSP(line, circles[j].p) - circles[j].r > EPS){
                }else{
                    isok = true;
                }
            }
            if(isok){
                cout << "Safe" << endl;
            }else{
                cout << "Danger" << endl;
            }
        }
    }
}

C++: #include <cstdio>
#include <cmath>

#define EPS 1e-10
int n,m;
double x[101],y[101],r[101];
double tx[101],ty[101],sx[101],sy[101];


struct vektor{
	double x,y;
	vektor(){}
	vektor(double xx,double yy){
		x=xx;
		y=yy;
	}

	double det(vektor p){
		return x*p.y-y*p.x;
	}
	double dot(vektor p){
		return x*p.x+y*p.y;
	}
	double len(){
		return sqrt(x*x+y*y);
	}
};


bool check(int v){
	for(int i=0;i<n;i++){
		double dt=sqrt((tx[v]-x[i])*(tx[v]-x[i])+(ty[v]-y[i])*(ty[v]-y[i]));
		double dr=sqrt((sx[v]-x[i])*(sx[v]-x[i])+(sy[v]-y[i])*(sy[v]-y[i]));
		if((dt-r[i])*(dr-r[i])<=0.0)return true;
		if(dt-r[i]>0 && dr-r[i]>0){
			vektor a=vektor(x[i]-sx[v],y[i]-sy[v]);
			vektor b=vektor(x[i]-tx[v],y[i]-ty[v]);
			vektor s=vektor(tx[v]-sx[v],ty[v]-sy[v]);
			double rr=fabs(s.det(a)/s.len());
			if(rr<=r[i]+EPS && s.dot(a)*b.dot(s)<=0)return true;
		}
	}
	return false;
}
int main(void){
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++){
			scanf("%lf %lf %lf",&x[i],&y[i],&r[i]);
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			scanf("%lf %lf %lf %lf",&tx[i],&ty[i],&sx[i],&sy[i]);
			printf("%s\n",check(i)?"Safe":"Danger");
		}
	}
	return 0;
}