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 <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

const double EPS=1e-10;

int Signum(double x)
{
	return abs(x)<EPS?0:x>0?1:-1;
}

typedef complex<double> Point;
struct Circle{
	Point c;
	double r;
	Circle(){}
	Circle(Point c,double r):c(c),r(r){}
};

double Dot(Point p1,Point p2)
{
	return real(conj(p1)*p2);
}

double Cross(Point p1,Point p2)
{
	return imag(conj(p1)*p2);
}

int CCW(Point a,Point b,Point c)
{
	Point d1=b-a,d2=c-a;
	double sign=Signum(Cross(d1,d2));
	if(sign)
		return sign;
	if(Dot(d1,d2)<-EPS)
		return -2;
	if(norm(d1)<norm(d2)-EPS)
		return 2;
	return 0;
}

Point Proj(Point a,Point b)
{
	return b*Dot(a,b)/norm(b);
}

string solve(Point t,Point o,const vector<Circle>& cirs)
{
	int n=cirs.size();
	rep(i,n){
		double r=cirs[i].r;
		Point c=cirs[i].c;
		bool out_t=r<abs(c-t);
		bool out_o=r<abs(c-o);
		if(out_t^out_o)
			return "Safe";
		if(out_t && out_o){
			Point p=t+Proj(c-t,o-t);
			if(abs(p-c)<r+EPS && CCW(t,p,o)==2)
				return "Safe";
		}
	}
	return "Danger";
}

int main()
{
	for(int n,m;cin>>n,n;){
		vector<Circle> cirs(n);
		rep(i,n)
			cin>>real(cirs[i].c)>>imag(cirs[i].c)>>cirs[i].r;
		cin>>m;
		vector<Point> t(m),o(m);
		rep(i,m)
			cin>>real(t[i])>>imag(t[i])>>real(o[i])>>imag(o[i]);
		
		rep(i,m)
			cout<<solve(t[i],o[i],cirs)<<endl;
	}
	
	return 0;
}

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

using namespace std;

int main() {
  int n;
  while(cin >> n, n != 0) {
    int m, wx[101], wy[101], wr[101];
    for(int i = 0; i < n; i++)  cin >> wx[i] >> wy[i] >> wr[i];
    cin >> m;
    while(m--) {
      int tx, ty, sx, sy, a, b, c, d;
      double e, len;
      cin >> tx >> ty >> sx >> sy;
      //|a*wx + b*wy + c| / テ「ツ按?(a*a)+ (b*b)) = len
      a = (tx - sx);
      b = 0 - (ty - sy);
      c = 0 - (a * ty + b * tx);
      e = (double)sqrt(a * a + b * b);
      
      int cnt = 0;
      for(int i = 0; i < n; i++) {
	
	if( (d = b * wx[i] + a * wy[i] + c ) < 0) d = 0 - d; 
	len  = d / e;
	
	if(len > wr[i]) cnt = 1; //Dangerテ、ツコツ、テァツつケ0
	else if ((wx[i] - tx)*(wx[i] - sx) > 0 && (wy[i] - ty)*(wy[i] - sy) > 0) cnt = 1; //テァツキツ堙・ツ按?、ツクツ甘」ツ?佚」ツつε」ツ?ェテ」ツ??ヲツ卍づ、ツコツ、テァツつケ0
	else {//テ、ツコツ、テァツつケテ」ツ?づ」ツつ?
	  double len_t = sqrt((tx - wx[i]) * (tx - wx[i]) + (ty - wy[i]) * (ty - wy[i])) , len_s = sqrt((sx - wx[i]) * (sx - wx[i]) + (sy - wy[i]) * (sy - wy[i]));
	  if(!((len_t < wr[i] && len_s < wr[i]))){ cnt = 2; break; } //テゥツャツシ, ティツ?ェテ・ツ按?」ツ?ゥテ」ツ?。テ」ツつ嘉」ツ?凝」ツ?古・ツ崢イテ」ツ?セテ」ツつ古」ツ?ヲテ」ツ??」ツつ凝」ツ?ェテ」ツつ唄afe
	}
      }
      if (cnt  == 2 || cnt == 0) cout << "Safe" << endl;
      else if( cnt == 1 ) cout << "Danger" << endl;
    }
  }
  return 0;
}