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 <bits/stdc++.h>
using namespace std;

// infty
#define ULLINF (ULLONG_MAX)
#define LLINF  (LLONG_MAX)
#define IINF   (INT_MAX)
#define INF    (1<<29)

// math
#define Sq(x) ((x)*(x))

// container utility
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair
#define PB push_back
#define EACH(it,c) for(__typeof((c).begin())it=(c).begin();it!=(c).end();it++)

// rep
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

// typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;

typedef long long ll;

// pair util
#define FST first
#define SND second

// range validator
#define CK(n,a,b) (a<=n && n<b)

// conversion
template<class T> inline string toStr(T a) { ostringstream oss_; oss_ << a; return oss_.str(); }
inline int toInt(string s) { return atoi(s.c_str()); }

// prime
bool isPrime(int a) { for(int i=2; i*i <=a; i++) if(a%i == 0) return false; return true; }

int const dx[] = {-1,0,1,0,-1,1,1,-1};
int const dy[] = {0,-1,0,1,-1,-1,1,1};

//////////////////////////////////////////////////////////////

#define EPS (1e-5)

typedef complex<double> P;
typedef pair<double, P> C;
typedef pair<P, P> S;

double dot(P a, P b) {
  return a.real()*b.real()+a.imag()*b.imag();
}

double cross(P a, P b) {
  return a.real()*b.imag()-a.imag()*b.real();
}

typedef S Line;
double distanceLP(Line l, P a) {
  P base = l.SND-l.FST;
  return cross(base, a-l.FST) / abs(base);
}

double distanceSP(S s, P a) {
  {
    P X = s.SND - s.FST, Y = a - s.FST;
    if(dot(X, Y) < EPS) return abs(Y);
  }
  {
    P X = s.FST - s.SND, Y = a - s.SND;
    if(dot(X, Y) < EPS) return abs(Y);
  }
  
  return distanceLP(s, a);
}

int main() {
  
  while(1) {
    int N;
    cin >> N; if(N == 0) break;
    
    vector<C> circles(N);
    
    for(int i=0; i<N; i++) {
      double x, y, r;
      cin >> x >> y >> r;
      circles[i] = C(r, P(x, y));
    }
    
    int Q; cin >> Q;
    while(Q--) {
      int x, y;
      cin >> x >> y; P taro(x, y);
      cin >> x >> y; P devil(x, y);
      
      bool ok = 0;
      for(int idx=0; idx<N; idx++) {
	if( distanceSP( S(taro, devil), circles[idx].SND ) < circles[idx].FST + EPS ) {
	  if( abs(taro-circles[idx].SND) + EPS > circles[idx].FST
	      || abs(devil-circles[idx].SND) + EPS > circles[idx].FST ) ok = 1;
	}
      }
      
      if(ok) cout << "Safe" << endl;
      else cout << "Danger" << endl;
    }
    
  }
  
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
typedef pair<P,int> PP;

bool isInCircle(double px,double py,double cx,double cy, double r){
	double a = pow(px - cx,2.0) + pow(py - cy,2.0);
	return a < r*r ? true : false;
}

bool isIntersectCircleAndLine(double px1,double py1,double px2, double py2,double cx,double cy, double r){
	if(isInCircle(px1,py1,cx,cy,r) && isInCircle(px2,py2,cx,cy,r)) return false;


	//vector1: from the center of the circle to the staring point
	double vx1 = (cx - px1);
	double vy1 = (cy - py1);

	//vector2: from the starting point to the ending point
	double vx2 = (px2 - px1);
	double vy2 = (py2 - py1);


	double n1=vx1*vx2 + vy1*vy2;
	double n2=vx2*vx2 + vy2*vy2;
	double n3=vx1*vx1 + vy1*vy1;
	

	if(n1 < 0){

		//distance between the starting point and the center of the circle
		return (pow(px1 - cx,2.0) + pow(py1 - cy,2.0) <= r*r) ? true : false;
	}

	else if(n1 > n2){
		//distance between the ending point and the center of the circle
		return (pow(px2 - cx,2.0) + pow(py2 - cy,2.0) <= r*r) ? true : false;
	}
	else{
		return (n3 - (n1/n2)*n1 <= r*r) ? true : false; 
	}
}

int main(){
	
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;
		vector<PP> wall;
		for(int i=0;i<n;i++){
			int x,y,r;
			scanf("%d %d %d",&x,&y,&r);
			wall.push_back(PP(P(x,y),r));
		}

		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			double tx,ty,sx,sy;
			scanf("%lf %lf %lf %lf",&tx,&ty,&sx,&sy);
			bool isSafe = false;
			for(int i=0;i<wall.size();i++){
				if(isIntersectCircleAndLine(tx,ty,sx,sy,wall[i].first.first,
					wall[i].first.second,wall[i].second)){
						isSafe = true;
				}
			}

			printf("%s\n",isSafe ? "Safe" : "Danger");
		}
	}
}

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

using namespace std;

#define NMAX 100

struct S1{
	int wx, wy, r;
};

/*点と円 true->点は円の内部*/
bool po_cr(int cx, int cy, int cr, int px, int py){
	return pow((cx - px), 2.0) + pow((cy - py), 2.0) <= pow(cr, 2.0);
}

/*円と線 true->接している*/
bool li_cr(int cx, int cy, int cr,int lx1, int ly1, int lx2, int ly2){

	double A, B, C, CA, AS, BS;
	double d;

	A = pow(cx - lx1, 2.0) + pow(cy - ly1, 2.0);
	B = pow(cx - lx2, 2.0) + pow(cy - ly2, 2.0);
	C = pow(lx1 - lx2, 2.0) + pow(ly1 - ly2, 2.0);
	CA = (lx2 - lx1) * (cy - ly1) - (cx - lx1) * (ly2 - ly1);
	AS = (cx - lx1) * (lx2 - lx1) + (cy - ly1) * (ly2 - ly1);
	BS = (cx - lx2) * (lx2 - lx1) + (cy - ly2) * (ly2 - ly1);

	if(C != 0) d = pow(CA, 2.0) / C;
	else d = 0;

	if(d <= pow(cr, 2.0)){
		if(AS * BS <= 0){
			return 1;
		}
		else if(pow(cr, 2.0) > A || pow(cr, 2.0) > B){
			return 1;
		}
	}
	return 0;
}

int main(){

	int n, m;
	S1 wall[NMAX];
	int tx, ty, sx, sy;
	bool f1, f2;

	while(1){

		cin >> n;
		if(n == 0) break;

		for(int i = 0; i < n; i++){
			cin >> wall[i].wx >> wall[i].wy >> wall[i].r;
		}

		cin >> m;

		for(int i = 0; i < m; i++){

			cin >> tx >> ty >> sx >> sy;

			f1 = true;
			for(int j = 0; j < n; j++){
				if(po_cr(wall[j].wx, wall[j].wy, wall[j].r, tx, ty)!=po_cr(wall[j].wx, wall[j].wy, wall[j].r, sx, sy)){
					f1 = false;
				}
			}
			
			f2 = true;
			for(int j = 0; j < n; j++){
				if(!po_cr(wall[j].wx, wall[j].wy, wall[j].r, sx, sy)){
					if(li_cr(wall[j].wx, wall[j].wy, wall[j].r, tx, ty, sx, sy)){
						f2 =  false;
					}
				}
			}

			if(f1 && f2 ) cout << "Danger" << endl;
			else cout << "Safe" << endl;
		}
	}

}