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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

int wx[100],wy[100],r[100];

main(){
  int n;
  while(cin>>n,n){
    rep(i,n)cin>>wx[i]>>wy[i]>>r[i];

    int m;
    cin>>m;
    while(m--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      bool safe=false;
      rep(i,n){
	if(a==c && b==d)break;
	int e=wx[i],f=wy[i],g=r[i];
	bool myin=(e-a)*(e-a)+(b-f)*(b-f)<g*g;
	bool enin=(e-c)*(e-c)+(f-d)*(f-d)<g*g;
	if(myin!=enin){
	  safe=true;
	  break;
	}
	if(myin && enin)continue;
	int var1=(-2*a*a*e+2*a*b*d-2*a*b*f+4*a*c*e-2*a*d*d+2*a*d*f-2*b*b*c+2*b*c*d+2*b*c*f-2*c*c*e-2*c*d*f);
	int var2=var1*var1-4*(a*a-2*a*c+b*b-2*b*d+c*c+d*d)*
	  (a*a*d*d-2*a*a*d*f+a*a*e*e+a*a*f*f-a*a*g*g-2*a*b*c*d+2*a*b*c*f+2*a*c*d*f
	   -2*a*c*e*e-2*a*c*f*f+2*a*c*g*g+b*b*c*c-2*b*c*c*f+c*c*e*e+c*c*f*f-c*c*g*g);
	if(var2<0)continue;
	int var3=2*a*a*e-2*a*b*d+2*a*b*f-4*a*c*e+2*a*d*d-2*a*d*f+2*b*b*c-2*b*c*d-2*b*c*f+
	  2*c*c*e+2*c*d*f;
	int var4=2*(a*a-2*a*c+b*b-2*b*d+c*c+d*d);
	double x1=double(sqrt(var2)+var3)/var4;
	double x2=double(-sqrt(var2)+var3)/var4;
	while(isnan(x1) || isnan(x2));
	if((a<=x1 && x1<=c) || (c<=x1 && x1<=a) || (a<=x2 && x2<=c) || (c<=x2 && x2<=a)){
	  safe=true;
	  break;
	}
      }
      if(safe)cout<<"Safe"<<endl;
      else cout<<"Danger"<<endl;
    }
  }
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
#include <list>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

#include <complex>
typedef complex<double> P;
typedef vector<P> vp;
typedef vector<double> vd;

#define LT(x,y) ((x)-(y)<=-EPS)
#define LE(x,y) ((x)-(y)<+EPS)

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();
}

double dist(P a, P b) {
	return abs(b-a);
}

double distance_ls_p(P a, P b, P c) {
	if(dot(b-a, c-a) < EPS) return abs(c-a);
	if(dot(a-b, c-b) < EPS) return abs(c-b);
	return abs(cross(b-a, c-a))/abs(b-a);
}

bool is_inner_circle_ls(P w, double r, P a, P b) {
	if(LT(dist(w, a), r) && LT(dist(w, b), r)) {
		return true;
	}
	return false;
}

bool is_cross_circle_ls(P w, double r, P a, P b) {
	if(is_inner_circle_ls(w, r, a, b)) {
		return false;
	} else {
		if(LE(distance_ls_p(a, b, w), r)) {
			return true;
		}
	}
	return false;
}

int main() {
	int n;
	while(cin >> n, n) {
		vp w(n);
		vd r(n);
		double x, y;
		REP(i, n) {
			cin >> x >> y >> r[i];
			w[i] = P(x, y);
		}

		int m;
		cin >> m;
		REP(i, m) {
			P t, s;
			cin >> x >> y;
			t = P(x, y);
			cin >> x >> y;
			s = P(x, y);

			bool safe = false;
			REP(i, n) {
				if(is_cross_circle_ls(w[i], r[i], t, s)) {
					cout << i << endl;
					safe = true;
					break;
				}
			}

			cout << (safe ? "Safe" : "Danger") << endl;
		}
	}

	return 0;
}