#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
int sgn(double x)
{
    if (x > eps) return 1;
    if (x < -eps) return -1;
    return 0;
}
struct Point
{
    double x, y;
    Point operator-(const Point &b) const { return Point{x - b.x, y - b.y}; }
    double operator^(const Point &b) const { return x * b.y - y * b.x; }
    double operator*(const Point &b) const { return x * b.x + y * b.y; }
};
struct Line
{
    Point s, e;
    int segcrossseg(Line v)
    {
        int d1 = sgn((e - s) ^ (v.s - s));
        int d2 = sgn((e - s) ^ (v.e - s));
        int d3 = sgn((v.e - v.s) ^ (s - v.s));
        int d4 = sgn((v.e - v.s) ^ (e - v.s));
        if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2) return 2;
        return (d1 == 0 && sgn((v.s - s) * (v.s - e)) <= 0) ||
               (d2 == 0 && sgn((v.e - s) * (v.e - e)) <= 0) ||
               (d3 == 0 && sgn((s - v.s) * (s - v.e)) <= 0) ||
               (d4 == 0 && sgn((e - v.s) * (e - v.e)) <= 0);
    }
};
const int N = 1050;
int fa[N], sz[N], lcnt;
Line ls[N];
void init()
{
    lcnt = 0;
    for (int i = 0; i < N; i++) fa[i] = i, sz[i] = 1;
}
int find(int x)
{
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void link(int x, int y)
{
    int fx = find(x), fy = find(y);
    if (fx ^ fy)
    {
        sz[fx] += sz[fy];
        fa[fy] = fx;
    }
}
int main()
{
    int T, n;
    scanf("%d", &T);
    while (T--)
    {
        init();
        char op[3];
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%s", op);
            if (*op == 'P')
            {
                scanf("%lf%lf%lf%lf", &ls[lcnt].s.x, &ls[lcnt].s.y, &ls[lcnt].e.x, &ls[lcnt].e.y);
                for (int l = 0; l < lcnt; l++)
                    if (ls[lcnt].segcrossseg(ls[l]))
                        link(l, lcnt);
                lcnt++;
            }
            if (*op == 'Q')
            {
                int k;
                scanf("%d", &k);
                printf("%d\n", sz[find(k - 1)]);
            }
        }
        if (T) putchar('\n');
    }
    return 0;
}
