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++: # define _CRT_SECURE_NO_WARNINGS 1
# include <iostream>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <map>
# include <stack>
using namespace std;
//# define M_PI 3.141592
# define INF ((int)(1<<30))
# define FOR(i,n) for(int i=0;i<(int)n;i++)
# define FORI(i,k,n) for(int i=k;i<(int)n;i++)
# define toRad 2.0*M_PI/360.0
# define inin(x) int x;cin>>x;
# define all(x) x.begin(),x.end()
# define debug(x) cout<<#x<<" :"<<x<<endl;
# define rep(i,n) for(int i=0;i<(int)n;i++)
# define EPS 1e-12
# define CHECK(i,a) FOR(i,a.size())cout<<#a<<"["<<i<<"] : "<<a[i]<<endl; 

class Point
{
public:
	double x, y;

	Point(double x = 0, double y = 0) :x(x), y(y) {}


	Point operator + (Point p) { return Point(x + p.x, y + p.y); }
	Point operator - (Point p) { return Point(x - p.x, y - p.y); }
	Point operator * (double k) { return Point(x * k, y * k); }
	Point operator / (double k) { return Point(x / k, y / k); }
	bool operator < (const Point &p)const { return x != p.x ? x < p.x : y < p.y; }
	bool operator ==(const Point &p)const { return fabs(x - p.x) < EPS&&fabs(y - p.y) < EPS; }

	double abs() { return sqrt(norm()); }
	double norm() { return x*x + y*y; }
};
typedef Point Vector;
struct Segment
{
	Point p1, p2;
};
typedef Segment Line;
class Circle
{
public:
	Point c;
	double r;
	Circle(Point c = Point(), double r = 0.0) :c(c), r(r) {}
};

double dot(Vector a, Vector b) { return a.x*b.x + a.y*b.y; }//??????
double cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x; }//??????
bool isOrthogonal(Vector a, Vector b) { return fabs(dot(a, b)) < EPS; }//??´??????
bool isOrthogonal(Point a1, Point a2, Point b1, Point b2) { return isOrthogonal(a1 - a2, b1 - b2); }
bool isOrthogonal(Line l1, Line l2) { return fabs(dot(l1.p2 - l1.p1, l2.p2 - l2.p1)) < EPS; }
bool isParallel(Vector a, Vector b) { return fabs(cross(a, b)) < EPS; }//?????????
bool isParallel(Point a1, Point a2, Point b1, Point b2) { return isParallel(a1 - a2, b1 - b2); }
bool isParallel(Line l1, Line l2) { return fabs(cross(l1.p2 - l1.p1, l2.p2 - l2.p1)) < EPS; }
Point project(Line s, Point p)
{
	Vector base = s.p2 - s.p1;
	double r = dot(p - s.p1, base) / base.norm();
	return s.p1 + base*r;
}



int main()
{
	int n;
	while (cin >> n&&n)
	{
		vector<Circle> a;
		for (int i = 0; i < n; i++)
		{
			Circle c;
			cin >> c.c.x >> c.c.y >> c.r;
			a.push_back(c);
		}
		int m; cin >> m;
		for (int i = 0; i < m; i++)
		{
			Line l;
			cin >> l.p1.x >> l.p1.y >> l.p2.x >> l.p2.y;
			bool ok = false;
			for (int j = 0; j < a.size(); j++)
			{
				double d = (project(l, a[j].c) - a[j].c).abs();
				if (d <= a[j].r) {
					Vector A, B, S;
					A = a[j].c - l.p1; B = a[j].c - l.p2; S = l.p2 - l.p1;
					if (A.abs() < a[j].r&&B.abs() < a[j].r)continue;
					if (dot(A, S)*dot(B, S) < EPS)ok = true;
					else if (fabs(a[j].r - A.abs()) < EPS || fabs(a[j].r - B.abs()) < EPS)ok = true;
				}
			}
			if (!ok)cout << "Danger" << endl;
			else cout << "Safe" << endl;
		}
	}
}

C++: #include <stdio.h>
#include <math.h>

int wx[100],wy[100],r[100],n,m,tx,ty,sx,sy,i,j,check,cos1,cos2;
double distance;

int main()
{
	for(;scanf("%d",&n),n;)
	{
		for(i=0;i<n;i++)
		scanf("%d %d %d",&wx[i],&wy[i],&r[i]);
		
		scanf("%d",&m);
		
		for(j=0;j<m;j++)
		{
			scanf("%d %d %d %d",&tx,&ty,&sx,&sy);
			
			check=0;
			for(i=0;i<n;i++)
			{
				cos1 = (wx[i]-tx)*(sx-tx)+(wy[i]-ty)*(sy-ty);
				cos2 = (wx[i]-sx)*(tx-sx)+(wy[i]-sy)*(ty-sy);
				
				distance = cos1<0 ? hypot(wx[i]-tx,wy[i]-ty) : cos2<0 ? hypot(wx[i]-sx,wy[i]-sy) : abs((sx-tx)*(wy[i]-ty)-(wx[i]-tx)*(sy-ty)) / hypot(sx-tx,sy-ty);
				
				if( (r[i]>hypot(tx-wx[i],ty-wy[i])) - (r[i]>hypot(sx-wx[i],sy-wy[i])) )
				check++;
				
				if(distance<=r[i]&& ( r[i]<hypot(tx-wx[i],ty-wy[i]) || r[i]<hypot(sx-wx[i],sy-wy[i]) ))
				check++;
			}
			puts(check ? "Safe" : "Danger");
		}
	}
	return 0;
}