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 <string>
#include <vector>
#include <algorithm>
#include <cmath>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define EPS 1e-8

using namespace std;

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

    Point operator+(const Point &o) const 
	{
        return Point(x+o.x, y+o.y);
    }

    Point operator-(const Point &o) const 
	{
        return Point(x-o.x, y-o.y);
    }
 
    Point operator*(const double m) const 
	{
        return Point(x*m, y*m);
    }
 
    Point operator/(const double d) const 
	{
        return Point(x/d, y/d);
    }

	bool operator < (const Point &a) const
	{
		return x == a.x ? y < a.y : x < a.x;
	}
 
    double cross(const Point &o) const 
	{
        return x * o.y - y * o.x;
    }
 
    double dot(const Point &o) const 
	{
        return x * o.x + y * o.y;
    }
 
    double atan() const 
	{
        return atan2(y, x);
    }

    double norm() const 
	{
        return dot(*this);
    }

	double distance (const Point &o) const 
	{
        return sqrt((x - o.x) * (x - o.x) + (y - o.y) * (y - o.y));
    }

	double distance_Line(const Point &a,const Point &b) const
	{
		Point p(x,y);

		Point d = b-a;

		if(d.x == 0 && d.y == 0)
		{
			return p.distance(a);
		}

		double r2 = d.x*d.x + d.y*d.y;
		double t = (d.x*(p.x - a.x) + d.y*(p.y - a.y)) / r2;

		if(t < 0)
		{
			return p.distance(a);
		}
		if(t > 1)
		{
			return p.distance(b);
		}

		Point c;
		c.x = (1-t) * a.x + t*b.x;
		c.y = (1-t) * a.y + t*b.y;
		return p.distance(c);
	}

	double area(const Point &a,const Point &b) const
	{
		double t = (a.x - x) * (b.y - y);
		double t2 = (a.y - y) * (b.x - x);
		double areas = abs(t-t2);
		return  areas/2;
	}
};


int main()
{
	int n;
	vector<Point> kp;
	vector<double> r;

	while(cin >> n && n)
	{
		double a,b,c,d;
		
		rep(i,n)
		{
			cin >> a>> b >> c;
			kp.push_back(Point(a,b));
			r.push_back(c);
		}

		int m;
		cin >> m;
		rep(i,m)
		{
			cin >> a >> b >> c >> d;
			Point p1(a,b);
			Point p2(c,d);

			bool flag = true;
			rep(j,kp.size())
			{
				double d1 = p1.distance(kp[j]);
				double d2 = p2.distance(kp[j]);

				//cout << "j:" << j << endl;

				if(d1 > r[j] && d2 < r[j])
				{
					flag = false;
					cout << "Safe" << endl;
					break;
				}
				else if(d1 < r[j] && d2 > r[j])
				{
					flag = false;
					cout << "Safe" << endl;
					break;
				}

				else if(d1 >= r[j] && d2 >= r[j])
				{
					double dd = kp[j].distance_Line(p1,p2);

					//cout << "dd:" << dd << endl;

					if(dd < r[j] + EPS)
					{
						flag = false;
						cout << "Safe" << endl;
						break;
					}
				}
			}
			if(flag)
			{
				std::cout << "Danger" << endl;
			}
		}


	}
	return 0;
}

C++: #include <iostream>
#include <complex>
#include <vector>
#include <algorithm>

#define EPS 1e-8

using namespace std;

typedef complex<double> P;

struct L : public vector<P> {
    L(const P &a, const P &b){
        push_back(a);
        push_back(b);
    }
};

struct C{
    P p;
    double r;
    C(const P &p, double r) : p(p), r(r) { }
};

double dot(const P &a, const P &b){
    return real(conj(a) * b);
}

bool intersectSP(const L &s, const P &p) {
    return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}

P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}

double distanceSP(const L &s, const P &p) {
  const P r = projection(s, p);
  if (intersectSP(s, r)) return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

int main(){
    int n,m;                                     
    int x,y,r;
    int a,b;
    vector<C> circles;
    while(cin >> n && n){
        for(int i=0;i<n;i++){
            cin >> x >> y >> r;
            circles.push_back(C(P(x,y), r));
        }
        cin >> m;
        for(int i=0;i<m;i++){
            bool isok = false;
            cin >> x >> y;
            cin >> a >> b;
            L line = L(P(x,y), P(a,b));
            for(int j=0;j<n;j++){
                if(distanceSP(line, circles[j].p) - circles[j].r > EPS){
                }else{
                    if(abs(line[0]-circles[j].p) - circles[j].r < EPS &&
                       abs(line[1]-circles[j].p) - circles[j].r < EPS){
                    }else{
                        isok = true;
                    }             
                }
            }
            if(isok){
                cout << "Safe" << endl;
            }else{
                cout << "Danger" << endl;
            }
        }
    }
}