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;
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define rrep(i,n) for(int i=(int)(n)-1; i>=0; i--)
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin(); it!=(c).end(); it++)
#define pb(a) push_back(a)
#define pr(a) cout << (a) << endl
#define PR(a,b) cout << (a) << " " << (b) << endl
#define F first
#define S second

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

typedef vector<P> G;
struct C {
  P p; double r;
  C(const P &p, double r) : p(p), r(r) { }
};

P proj(P p, L l){
  return l.first + dot(p - l.first, l.second - l.first) / norm(l.second - l.first) * (l.second - l.first);
}

bool isIntersectSP(L s, P p) {
  return abs(s.F-p)+abs(s.S-p)-abs(s.S-s.F) < EPS;
}

vector<P> getIntersectCS(C c, L s){
  vector<P> res;
  P h = proj(c.p, s);
  double d = abs(c.p - h);
  if(d > c.r + EPS);
  else if(d > c.r - EPS){
    if(isIntersectSP(s,h)) res.push_back(h);
  }
  else {
    P v = s.second - s.first;
    v = (sqrt(c.r*c.r - d*d) / abs(v)) * v;
    if(isIntersectSP(s, h+v)) res.push_back(h+v);
    if(isIntersectSP(s,h-v)) res.push_back(h-v);
  }
  return res;
}

int main() {
  int n;
  while(cin >> n && n) {
    vector<C> c;
    rep(i,n) {
      double x,y,r;
      cin >> x >> y >> r;
      c.pb(C(P(x,y),r));
    }
    int m;
    cin >> m;
    rep(j,m) {
      L l;
      double x1,x2,y1,y2;
      cin >> x1 >> y1 >> x2 >> y2;
      l.F.real(x1);
      l.F.imag(y1);
      l.S.real(x2);
      l.S.imag(y2);
      int cnt=0;
      rep(i,n) {
	vector<P> v=getIntersectCS(c[i],l);
	cnt+=v.size();
      }
      if(!cnt) pr("Danger");
      else pr("Safe");
    }
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<cmath>
#include<cstdlib>
using namespace std;
struct Point{
	Point():x(0),y(0){}
	Point(int x,int y):x(x),y(y){}
	int x,y;
};
struct Circle{
	Point p;
	int r;
};
/* _a,b©ç£ÌñæðÔ·Ö */
int DistanceSquared( Point a, Point b ){
	return ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y );
}
/* ¾YASA éÇÌîñ©çA»êçÌÊuÖWªSafe©Danger©²×éÖ */
bool IsSafePlace( Circle c, Point taro, Point dem ){
	double vx1, vy1, vx2, vy2, alpha1, alpha2;
	int seki = ( (taro.x - c.p.x) * ( dem.x - taro.x ) + (taro.y - c.p.y) * (dem.y - taro.y ) );
	int D = seki * seki - DistanceSquared(taro,dem) * ( DistanceSquared(c.p,taro) - c.r * c.r );

	if( D < 0 )
		return false;
	if( D == 0 )
		return true;

	//cout << DistanceSquared(taro, dem) << endl;

	alpha1 = ( -seki - sqrt((double)D) ) / DistanceSquared(taro, dem);
	alpha2 = ( -seki + sqrt((double)D) ) / DistanceSquared(taro, dem);

	//cout << alpha1 << "," << alpha2 << endl;

	vx1 = (taro.x - c.p.x) + alpha1 * ( dem.x - taro.x );
	vy1 = (taro.y - c.p.y) + alpha1 * ( dem.y - taro.y );
	vx2 = (taro.x - c.p.x) + alpha2 * ( dem.x - taro.x );
	vy2 = (taro.y - c.p.y) + alpha2 * ( dem.y - taro.y );

	//cout << px + c.p.x << ' ' << py + c.p.y << endl;

	if( ( vx1 * (taro.y - c.p.y) - (taro.x - c.p.x) * vy1 ) *
		( vx1 * (dem.y  - c.p.y) - (dem.x  - c.p.x) * vy1 ) > 0 &&
		( vx2 * (taro.y - c.p.y) - (taro.x - c.p.x) * vy2 ) *
		( vx2 * (dem.y  - c.p.y) - (dem.x  - c.p.x) * vy2 ) > 0)
		return false;
	else
		return true;
}
int main(void){
	while(true){
		int n, m;
		vector<Circle> vWall;

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

		for(int i = 0; i < n; ++i){
			Circle tc;
			cin >> tc.p.x >> tc.p.y >> tc.r;
			vWall.push_back( tc );
		}

		cin >> m;

		for(int i = 0; i < m; ++i){
			bool safe = false;
			Point p_taro, p_daemon;
			cin >> p_taro.x >> p_taro.y >> p_daemon.x >> p_daemon.y;

			for( unsigned int j = 0; j < vWall.size(); ++j ){
				if( IsSafePlace( vWall[j], p_taro, p_daemon ) ){
					safe = true;
					cout << "Safe\n";
					break;
				}
			}
			if( !safe ){
				cout << "Danger\n";
			}
		}
	}
}

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{
		if(n2<=0) return (n3*n2 - n1*n1 >= r*r*n2) ? true : false; 
		else return (n3*n2 - n1*n1 <= r*r*n2) ? 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");
		}
	}
}