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 <complex>
#include <vector>
#include <algorithm>

#define EPS 1e-9

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 &&
                    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 <stdio.h>
#include <cstdlib>
#include <math.h>

int wx[100],wy[100],r[100],n,m,tx,ty,sx,sy,i,j,check,cos1,cos2;
double distance;

int main()
{
	for(;scanf("%d",&n),n;)
	{
		for(i=0;i<n;i++)
		scanf("%d %d %d",&wx[i],&wy[i],&r[i]);
		
		scanf("%d",&m);
		
		for(j=0;j<m;j++)
		{
			scanf("%d %d %d %d",&tx,&ty,&sx,&sy);
			
			check=0;
			for(i=0;i<n;i++)
			{
				cos1 = (wx[i]-tx)*(sx-tx)+(wy[i]-ty)*(sy-ty);
				cos2 = (wx[i]-sx)*(tx-sx)+(wy[i]-sy)*(ty-sy);
				
				distance = cos1<0 ? hypot(wx[i]-tx,wy[i]-ty) : cos2<0 ? hypot(wx[i]-sx,wy[i]-sy) : abs((sx-tx)*(wy[i]-ty)-(wx[i]-tx)*(sy-ty)) / hypot(sx-tx,sy-ty);
				
				if( (r[i]>hypot(tx-wx[i],ty-wy[i])) - (r[i]>hypot(sx-wx[i],sy-wy[i])) )
				check++;
				
				if(distance<=r[i]&& ( r[i]<hypot(tx-wx[i],ty-wy[i]) || r[i]<hypot(sx-wx[i],sy-wy[i]) ))
				check++;
			}
			puts(check ? "Safe" : "Danger");
		}
	}
	return 0;
}

C++: /**************** Geometrical Library ****************/

#include<cmath>
#include<vector>
#include<algorithm>

#define	EPS	1e-9

enum {CCW=1,CW=-1,ON=0};

#define	mp	make_pair
#define	pb	push_back

using namespace std;

class Point{
public:
	double x,y;
	Point(){}
	Point(double xx,double yy):x(xx),y(yy){}
};

class Line:public vector<Point>{
public:
	Line(const Point &a,const Point &b){
		pb(a),pb(b);
	}
};

class Segment:public Line{
public:
	Segment(const Point &a,const Point &b):Line(a,b){}
};

class Polygon:public vector<Point>{};

class Circle{
public:
	Point c;
	double r;
	Circle(){}
	Circle(const Point &cc,double rr):c(cc),r(rr){}
};

Point &operator +=(Point &a,const Point &b){
	a.x+=b.x,a.y+=b.y;
	return a;
}

Point &operator -=(Point &a,const Point &b){
	a.x-=b.x,a.y-=b.y;
	return a;
}

Point &operator *=(Point &a,double c){
	a.x*=c,a.y*=c;
	return a;
}

Point &operator /=(Point &a,double c){
	a.x/=c,a.y/=c;
	return a;
}

Point operator +(const Point &a,const Point &b){
	Point c=a;
	return c+=b;
}

Point operator -(const Point &a,const Point &b){
	Point c=a;
	return c-=b;
}

Point operator *(double c,const Point &a){
	Point b=a;
	return b*=c;
}

Point operator /(const Point &a,double c){
	Point b=a;
	return b/=c;
}

bool operator <(const Point &a,const Point &b){
	return (a.x==b.x)?(a.y<b.y):(a.x<b.x);
}

bool operator >(const Point &a,const Point &b){
	return b<a;
}

double dot(const Point &a,const Point &b){
	return a.x*b.x+a.y*b.y;
}

double cross(const Point &a,const Point &b){
	return a.x*b.y-a.y*b.x;
}

double norm2(const Point &a){
	return a.x*a.x+a.y*a.y;
}

int ccw(const Point &a,Point b,Point c){
	b-=a,c-=a;
	double rotdir=cross(b,c);
	if(rotdir>EPS)		return CCW;
	if(rotdir<-EPS)		return CW;
	return ON;
}

inline void calc_abc(const Line &l,double &a,double &b,double &c){	// l : ax+by+c=0
	a=l[0].y-l[1].y;
	b=l[1].x-l[0].x;
	c=l[0].x*l[1].y-l[1].x*l[0].y;
}

Point perp_foot(const Point &p,const Line &l){
	double a,b,c;
	calc_abc(l,a,b,c);
	return p-(a*p.x+b*p.y+c)/(a*a+b*b)*Point(a,b);
}

int intersect(const Circle &c,const Line &l,Point *p=NULL,Point *q=NULL){
	Point m=perp_foot(c.c,l);
	double d2=norm2(c.c-m);
	if(c.r*c.r+EPS<d2)	return 0;
	else if(abs(c.r*c.r-d2)<EPS){
		if(p&&q)	*p=*q=m;
		return 1;
	}
	else{
		if(p&&q){
			Point v=l[0]-m;
			v*=sqrt((c.r*c.r-d2)/norm2(v));
			*p=m+v,*q=m-v;
		}
		return 2;
	}
}

int intersect(const Circle &c,const Segment &s,Point *p=NULL,Point *q=NULL){
	Point pp,qq;
	int n=intersect(c,Line(s[0],s[1]),&pp,&qq);
	if(n==0)	return 0;
	else if(n==1){
		if(dot(pp-s[0],pp-s[1])<EPS){
			if(p&&q)	*p=*q=pp;
			return 1;
		}
		return 0;
	}
	else{
		bool bp=(dot(pp-s[0],pp-s[1])<EPS);
		bool bq=(dot(qq-s[0],qq-s[1])<EPS);
		if(bp&&bq){
			if(p&&q)	*p=pp,*q=qq;
			return 2;
		}
		if(bp||bq){
			if(p&&q)	*p=(bp?pp:qq);
			return 1;
		}
		return 0;
	}
}

/**************** Library END ****************/

#include<cstdio>

int main(){
	for(int n;scanf("%d",&n),n;){
		Circle wall[100];
		for(int i=0;i<n;i++){
			int x,y,r;	scanf("%d%d%d",&x,&y,&r);
			wall[i]=Circle(Point(x,y),r);
		}

		int m;	scanf("%d",&m);
		while(m--){
			Point taro,oni;	scanf("%lf%lf%lf%lf",&taro.x,&taro.y,&oni.x,&oni.y);
			Segment view(taro,oni);
			bool ok=false;
			for(int i=0;i<n;i++){
				if(intersect(wall[i],view)>0){
					ok=true;
					break;
				}
			}
			puts(ok?"Safe":"Danger");
		}
	}

	return 0;
}