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);}
}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(b-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++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
#include <complex>
int sign(double x){
  if(x > EPS) return 1;
  if(x < -EPS) return -1;
  return 0;
}
typedef complex<double> Point;
struct Line : public vector<Point> {
  Line(const Point& a, const Point& b){
    push_back(a); push_back(b);
  }
  Point vector() const {
    return back() - front();
  }
};
struct Circle{
  Point c;
  double r;
  Circle() {}
  Circle(Point c, double r) : c(c), r(r) {}
};

double dot(Point a, Point b){
  return real(conj(a) * b);
}
double cross(Point a, Point b){
  return imag(conj(a) * b);
}

int ccw(Point a, Point b, Point c){
  b -= a; c -= a;
  if(cross(b, c) > EPS) return +1;
  if(cross(b, c) < -EPS) return -1;
  if(dot(b, c) < 0) return +2;
  if(norm(b) < norm(c)) return -2;
  return 0;
}


bool intersectSP(const Line& s, const Point& p){
  return ccw(s[0], s[1], p) == 0;
}

Point projection(const Line& l, const Point& p){
  double t = dot(p - l[0], l.vector()) / norm(l.vector());
  return l[0] + t * l.vector();
}

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

enum{ OUT, ON, IN };
int contains(const Circle& C, const Point& p){
  double d = abs(C.c - p);
  if(sign(d - C.r) > 0) return OUT;
  if(sign(d - C.r) == 0) return ON;
  return IN;
}

bool intersectCS(const Circle& C, const Line& l){
  int b[2];
  REP(i, 2) b[i] = contains(C, l[i]);
  if(b[0] > b[1]) swap(b[0], b[1]);
  if(b[0] == OUT && b[1] == IN) return true;
  if(b[0] == IN && b[1] == IN) return false;
  if(b[0] == ON) return true;
  double d = distanceSP(l, C.c);
  if(sign(d - C.r) > 0) return false;
  return true;
}

int main(){
  int N;
  while(cin >> N && N){
    vector<Circle> WALL(N);
    REP(i, N) cin >> WALL[i].c.real() >> WALL[i].c.imag() >> WALL[i].r;
    int M;
    cin >> M;
    Point p[2];
    REP(i, M){
      REP(j, 2) cin >> p[j].real() >> p[j].imag();
      bool danger = true;
      REP(j, N) if(intersectCS(WALL[j], Line(p[0], p[1]))) danger = false;
      if(danger) cout << "Danger" << endl;
      else cout << "Safe" << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>

using namespace std;

int main(){
	
	while(true){
		
		int n, m;
		
		cin >> n;
		
		if(n == 0){
			break;
		}
		
		long long int wx[200], wy[200], r[200];
		
		for(int i = 0; i < n; i++){
			cin >> wx[i] >> wy[i] >> r[i];
		}
		
		cin >> m;
		
		for(int loop = 0; loop < m; loop++){
			
			bool flag = false;
			
			long long int tx, ty, sx, sy;
			cin >> tx >> ty >> sx >> sy;
			
			for(int i = 0; i < n; i++){
				bool isin1 = false, isin2 = false;
				if((sx - wx[i]) * (sx - wx[i]) + (sy - wy[i]) * (sy - wy[i]) < r[i] * r[i]){
					isin1 = true;
				}
				if((tx - wx[i]) * (tx - wx[i]) + (ty - wy[i]) * (ty - wy[i]) < r[i] * r[i]){
					isin2 = true;
				}
				if(isin1 && isin2){
					continue;
				}else if(isin1 || isin2){
					flag = true;
					break;
				}
				long long int a, b, c;
				a = (ty - sy);
				b = (sx - tx);
				c = sx * (sy - ty) - sy * (sx - tx);
				if((a * wx[i] + b * wy[i] + c) * (a * wx[i] + b * wy[i] + c) > r[i] * r[i] * (a * a + b * b)){
					continue;
				}
				double a1, b1, x_center;
				a1 = (sx - tx) * (sx - tx) + (sy - ty) * (sy - ty);
				b1 = (sx - tx) * (wx[i] * (sx - tx) + wy[i] * (sy - ty)) - (sx * ty - tx * sy) * (sy - ty);
				x_center = b1 / a1;
				if((sx - x_center) * (tx - x_center) <= 0){
					flag = true;
					break;
				}
			}
			if(flag){
				cout << "Safe" << endl;
			}else{
				cout << "Danger" << endl;
			}
		}
		
	}
	return 0;
}