Problem: Like all children, Alesha loves New Year celebration. During the celebration he and his whole family dress up the fir-tree. Like all children, Alesha likes to play with garlands — chains consisting of a lightbulbs.

Alesha uses a grid field sized n × m for playing. The rows of the field are numbered from 1 to n from the top to the bottom and columns are numbered from 1 to m from the left to the right.

Alesha has k garlands which he places at the field. He does so in the way such that each lightbulb of each garland lies in the center of some cell in the field, and each cell contains at most one lightbulb. Of course lightbulbs, which are neighbours in some garland, appears in cells neighbouring by a side.

<image>

The example of garland placing.

Each garland is turned off or turned on at any moment. If some garland is turned on then each of its lightbulbs is turned on, the same applies for garland turned off. Each lightbulb in the whole garland set is unique, and thus, being turned on, brings Alesha some pleasure, described by an integer value. Turned off lightbulbs don't bring Alesha any pleasure.

Alesha can turn garlands on and off and wants to know the sum of pleasure value which the lightbulbs, placed in the centers of the cells in some rectangular part of the field, bring him. Initially all the garlands are turned on.

Alesha is still very little and can't add big numbers. He extremely asks you to help him.

Input

The first line of the input contains three integers n, m and k (1 ≤ n, m, k ≤ 2000) — the number of field rows, the number of field columns and the number of garlands placed at the field respectively.

Next lines contains garlands set description in the following format:

The first line of a single garland description contains a single integer len (1 ≤ len ≤ 2000) — the number of lightbulbs in the garland.

Each of the next len lines contains three integers i, j and w (1 ≤ i ≤ n, 1 ≤ j ≤ m, 1 ≤ w ≤ 109) — the coordinates of the cell containing a lightbullb and pleasure value Alesha gets from it if it is turned on. The lightbulbs are given in the order they are forming a chain in the garland. It is guaranteed that neighbouring lightbulbs are placed in the cells neighbouring by a side.

The next line contains single integer q (1 ≤ q ≤ 106) — the number of events in Alesha's game. The next q lines describes events in chronological order. The i-th of them describes the i-th event in the one of the following formats:

  * SWITCH i — Alesha turns off i-th garland if it is turned on, or turns it on if it is turned off. It is guaranteed that 1 ≤ i ≤ k. 
  * ASK x1 y1 x2 y2 — Alesha wants to know the sum of pleasure values the lightbulbs, placed in a rectangular part of the field. Top-left cell of a part has coordinates (x1, y1) and right-bottom cell has coordinates (x2, y2). It is guaranteed that 1 ≤ x1 ≤ x2 ≤ n and 1 ≤ y1 ≤ y2 ≤ m. There is no more than 2000 events of this type in the input. 



All the numbers in the input are integers.

Please note that the input is quite large, so be careful while using some input ways. In particular, it's not recommended to use cin in codes on C++ and class Scanner in codes on Java.

Output

For each ASK operation print the sum Alesha wants to know in a separate line. Print the answers in chronological order.

Examples

Input

4 4 3
5
1 1 2
1 2 3
2 2 1
2 1 4
3 1 7
4
1 3 1
2 3 3
2 4 3
1 4 1
7
4 1 1
4 2 9
3 2 8
3 3 3
4 3 4
4 4 1
3 4 1
2
ASK 2 2 3 3
ASK 1 1 4 4


Output

15
52


Input

4 4 1
8
4 1 1
3 1 2
2 1 1
1 1 7
1 2 5
2 2 4
2 3 1
1 3 1
3
ASK 1 1 3 2
SWITCH 1
ASK 1 1 3 2


Output

19
0

Note

<image>

This image illustrates the first sample case.

Solution (in JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.NoSuchElementException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        static OutputWriter out;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
//        System.err.println(2e3 * (4 + 8) *  2e3 * (Math.log(2e3) / Math.log(2))  / Math.pow(1024, 2));

            TaskE.out = out;
            int n;
            int m;
            int countGarland;
            int[][] x;
            int[][] y;
            int[][] cost;
            n = in.readInt();
            m = in.readInt();
            countGarland = in.readInt();
            x = new int[countGarland][];
            y = new int[countGarland][];
            cost = new int[countGarland][];

            for (int i = 0; i < countGarland; i++) {
                int len = in.readInt();
                x[i] = new int[len];
                y[i] = new int[len];
                cost[i] = new int[len];
                IOUtils.readIntArrays(in, x[i], y[i], cost[i]);
                //trees[i] = new TaskE.SegTree(ArrayUtils.order(x[i]), x[i], y[i], cost[i]);
            }

            int q = in.readInt();
            boolean[] isAsk = new boolean[q];
            long[] answers = new long[q];
            IntArrayList[] qwer = new IntArrayList[countGarland];
            for (int i = 0; i < countGarland; i++) {
                qwer[i] = new IntArrayList();
            }
            boolean[] isOn = new boolean[countGarland];
            Arrays.fill(isOn, true);
            int[] x0 = new int[q];
            int[] x1 = new int[q];
            int[] y0 = new int[q];
            int[] y1 = new int[q];
            for (int iter = 0; iter < q; iter++) {
                String s = in.readString();
                if (s.charAt(0) == 'S') {
                    int index = in.readInt() - 1;
                    isOn[index] ^= true;
                } else {
                    isAsk[iter] = true;
                    x0[iter] = in.readInt();
                    y0[iter] = in.readInt();
                    x1[iter] = in.readInt();
                    y1[iter] = in.readInt();
                    for (int i = 0; i < countGarland; i++) {
                        if (isOn[i]) {
                            qwer[i].add(iter);
                        }
                    }
                }
            }

            for (int g = 0; g < countGarland; g++) {
                TaskE.SegTree seg = new TaskE.SegTree(ArrayUtils.order(x[g]), x[g], y[g], cost[g]);
                for (int i = 0; i < qwer[g].size(); i++) {
                    int idQ = qwer[g].get(i);
                    answers[idQ] += seg.sum(x0[idQ], x1[idQ], y0[idQ], y1[idQ]);
                }
            }
            for (int i = 0; i < q; i++) {
                if (isAsk[i]) {
                    out.printLine(answers[i]);
                }
            }
        }

        static class SegTree {
            int n;
            int[] orderByX;
            int[] x;
            int[] y;
            int[] cost;
            int[][] treeOrderByY;
            long[][] treePartSum;

            public SegTree(int[] orderByX, int[] x, int[] y, int[] cost) {
                this.orderByX = orderByX;
                this.x = x;
                this.y = y;
                this.cost = cost;
                n = orderByX.length;
                int max = Math.max(1, Integer.highestOneBit(n) << 2);
                treeOrderByY = new int[max][];
                treePartSum = new long[max][];
                init(0, 0, n - 1);
            }

            private void init(int v, int tl, int tr) {
                treeOrderByY[v] = new int[tr - tl + 1];
                treePartSum[v] = new long[tr - tl + 1];
                if (tl == tr) {
                    treeOrderByY[v][0] = orderByX[tl];
                    treePartSum[v][0] = cost[orderByX[tl]];
                } else {
                    int tm = (tl + tr) >> 1;
                    init(v + v + 1, tl, tm);
                    init(v + v + 2, tm + 1, tr);
                    merge(treeOrderByY[v], treePartSum[v], treeOrderByY[v + v + 1], treeOrderByY[v + v + 2]);
                }
            }

            private void merge(int[] order, long[] sum, int[] orderL, int[] orderR) {
                int l = 0;
                int r = 0;
                for (int pos = 0; pos < order.length; pos++) {
                    if (l < orderL.length && r < orderR.length) {
                        if (y[orderL[l]] < y[orderR[r]]) {
                            order[pos] = orderL[l++];
                        } else {
                            order[pos] = orderR[r++];
                        }
                    } else if (l < orderL.length) {
                        order[pos] = orderL[l++];
                    } else {
                        order[pos] = orderR[r++];
                    }
                }
                for (int pos = 0; pos < order.length; pos++) {
                    sum[pos] = cost[order[pos]];
                    if (pos != 0) {
                        sum[pos] += sum[pos - 1];
                    }
                }
            }

            long sum(int x0, int x1, int y0, int y1) {
                return sum(x0, x1, y0, y1, 0, 0, n - 1);
            }

            private long sum(int x0, int x1, int y0, int y1, int v, int tl, int tr) {
                int xtl = x[orderByX[tl]];
                int xtr = x[orderByX[tr]];
                if (x1 < xtl || xtr < x0) {
                    return 0;
                }

                if (x0 <= xtl && xtr <= x1) {
                    int posL = firstBigger(y0 - 1, treeOrderByY[v]);
                    int posR = lastLess(y1 + 1, treeOrderByY[v]);
                    long res = posL <= posR ? treePartSum[v][posR] - (posL == 0 ? 0 : treePartSum[v][posL - 1]) : 0;
                    return res;
                } else {
                    int tm = (tl + tr) >>> 1;
                    return sum(x0, x1, y0, y1, v + v + 1, tl, tm) +
                            sum(x0, x1, y0, y1, v + v + 2, tm + 1, tr);
                }
            }

            private int lastLess(int y, int[] orderByY) {
                int l = -1, r = orderByY.length - 1;
                while (l < r) {
                    int m = (l + r + 1) >>> 1;
                    if (this.y[orderByY[m]] < y) {
                        l = m;
                    } else {
                        r = m - 1;
                    }
                }
                return l;
            }

            private int firstBigger(int y, int[] orderByY) {
                int l = 0, r = orderByY.length;
                while (l < r) {
                    int m = (l + r) >>> 1;
                    if (y < this.y[orderByY[m]]) {
                        r = m;
                    } else {
                        l = m + 1;
                    }
                }
                return l;
            }

        }

    }

    static class ArrayUtils {
        public static int[] range(int from, int to) {
            return Range.range(from, to).toArray();
        }

        public static int[] createOrder(int size) {
            return range(0, size);
        }

        public static int[] sort(int[] array, IntComparator comparator) {
            return sort(array, 0, array.length, comparator);
        }

        public static int[] sort(int[] array, int from, int to, IntComparator comparator) {
            if ((from == 0) && (to == array.length)) {
                new IntArray(array).sort(comparator);
            } else {
                new IntArray(array).subList(from, to).sort(comparator);
            }
            return array;
        }

        public static int[] order(final int[] array) {
            return sort(createOrder(array.length), new IntComparator() {
                public int compare(int first, int second) {
                    if (array[first] < array[second]) {
                        return -1;
                    }
                    if (array[first] > array[second]) {
                        return 1;
                    }
                    return 0;
                }
            });
        }

    }

    static interface IntIterator {
        public int value() throws NoSuchElementException;

        public boolean advance();

        public boolean isValid();

    }

    static interface IntReversableCollection extends IntCollection {
    }

    static class IntArray extends IntAbstractStream implements IntList {
        private int[] data;

        public IntArray(int[] arr) {
            data = arr;
        }

        public int size() {
            return data.length;
        }

        public int get(int at) {
            return data[at];
        }

        public void addAt(int index, int value) {
            throw new UnsupportedOperationException();
        }

        public void removeAt(int index) {
            throw new UnsupportedOperationException();
        }

        public void set(int index, int value) {
            data[index] = value;
        }

    }

    static interface IntList extends IntReversableCollection {
        public abstract int get(int index);

        public abstract void set(int index, int value);

        public abstract void addAt(int index, int value);

        public abstract void removeAt(int index);

        default public void swap(int first, int second) {
            if (first == second) {
                return;
            }
            int temp = get(first);
            set(first, get(second));
            set(second, temp);
        }

        default public IntIterator intIterator() {
            return new IntIterator() {
                private int at;
                private boolean removed;

                public int value() {
                    if (removed) {
                        throw new IllegalStateException();
                    }
                    return get(at);
                }

                public boolean advance() {
                    at++;
                    removed = false;
                    return isValid();
                }

                public boolean isValid() {
                    return !removed && (at < size());
                }

                public void remove() {
                    removeAt(at);
                    at--;
                    removed = true;
                }
            };
        }


        default public void add(int value) {
            addAt(size(), value);
        }

        default public IntList sort(IntComparator comparator) {
            Sorter.sort(this, comparator);
            return this;
        }

        default public IntList subList(final int from, final int to) {
            return new IntList() {
                private final int shift;
                private final int size;

                {
                    if ((from < 0) || (from > to) || (to > IntList.this.size())) {
                        throw new IndexOutOfBoundsException("from = " + from + ", to = " + to + ", size = " + size());
                    }
                    shift = from;
                    size = to - from;
                }

                public int size() {
                    return size;
                }

                public int get(int at) {
                    if ((at < 0) || (at >= size)) {
                        throw new IndexOutOfBoundsException("at = " + at + ", size = " + size());
                    }
                    return IntList.this.get(at + shift);
                }

                public void addAt(int index, int value) {
                    throw new UnsupportedOperationException();
                }

                public void removeAt(int index) {
                    throw new UnsupportedOperationException();
                }

                public void set(int at, int value) {
                    if ((at < 0) || (at >= size)) {
                        throw new IndexOutOfBoundsException("at = " + at + ", size = " + size());
                    }
                    IntList.this.set(at + shift, value);
                }

                public IntList compute() {
                    return new IntArrayList(this);
                }
            };
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if ((c < '0') || (c > '9')) {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                if (Character.isValidCodePoint(c)) {
                    res.appendCodePoint(c);
                }
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return (c == ' ') || (c == '\n') || (c == '\r') || (c == '\t') || (c == -1);
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static abstract class IntAbstractStream implements IntStream {

        public String toString() {
            StringBuilder builder = new StringBuilder();
            boolean first = true;
            for (IntIterator it = intIterator(); it.isValid(); it.advance()) {
                if (first) {
                    first = false;
                } else {
                    builder.append(' ');
                }
                builder.append(it.value());
            }
            return builder.toString();
        }


        public boolean equals(Object o) {
            if (!(o instanceof IntStream)) {
                return false;
            }
            IntStream c = (IntStream) o;
            IntIterator it = intIterator();
            IntIterator jt = c.intIterator();
            while (it.isValid() && jt.isValid()) {
                if (it.value() != jt.value()) {
                    return false;
                }
                it.advance();
                jt.advance();
            }
            return !it.isValid() && !jt.isValid();
        }


        public int hashCode() {
            int result = 0;
            for (IntIterator it = intIterator(); it.isValid(); it.advance()) {
                result *= 31;
                result += it.value();
            }
            return result;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(long i) {
            writer.println(i);
        }

    }

    static class IntArrayList extends IntAbstractStream implements IntList {
        private int size;
        private int[] data;

        public IntArrayList() {
            this(3);
        }

        public IntArrayList(int capacity) {
            data = new int[capacity];
        }

        public IntArrayList(IntCollection c) {
            this(c.size());
            addAll(c);
        }

        public IntArrayList(IntStream c) {
            this();
            if (c instanceof IntCollection) {
                ensureCapacity(((IntCollection) c).size());
            }
            addAll(c);
        }

        public IntArrayList(IntArrayList c) {
            size = c.size();
            data = c.data.clone();
        }

        public IntArrayList(int[] arr) {
            size = arr.length;
            data = arr.clone();
        }

        public int size() {
            return size;
        }

        public int get(int at) {
            if (at >= size) {
                throw new IndexOutOfBoundsException("at = " + at + ", size = " + size);
            }
            return data[at];
        }

        private void ensureCapacity(int capacity) {
            if (data.length >= capacity) {
                return;
            }
            capacity = Math.max(2 * data.length, capacity);
            data = Arrays.copyOf(data, capacity);
        }

        public void addAt(int index, int value) {
            ensureCapacity(size + 1);
            if ((index > size) || (index < 0)) {
                throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
            }
            if (index != size) {
                System.arraycopy(data, index, data, index + 1, size - index);
            }
            data[index] = value;
            size++;
        }

        public void removeAt(int index) {
            if ((index >= size) || (index < 0)) {
                throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
            }
            if (index != (size - 1)) {
                System.arraycopy(data, index + 1, data, index, size - index - 1);
            }
            size--;
        }

        public void set(int index, int value) {
            if (index >= size) {
                throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
            }
            data[index] = value;
        }

    }

    static class IOUtils {
        public static void readIntArrays(InputReader in, int[]... arrays) {
            for (int i = 0; i < arrays[0].length; i++) {
                for (int j = 0; j < arrays.length; j++) {
                    arrays[j][i] = in.readInt();
                }
            }
        }

    }

    static interface IntCollection extends IntStream {
        public int size();

        default public void add(int value) {
            throw new UnsupportedOperationException();
        }

        default public int[] toArray() {
            int size = size();
            int[] array = new int[size];
            int i = 0;
            for (IntIterator it = intIterator(); it.isValid(); it.advance()) {
                array[i++] = it.value();
            }
            return array;
        }

        default public IntCollection addAll(IntStream values) {
            for (IntIterator it = values.intIterator(); it.isValid(); it.advance()) {
                add(it.value());
            }
            return this;
        }

    }

    static interface IntStream extends Iterable<Integer>, Comparable<IntStream> {
        public IntIterator intIterator();

        default public Iterator<Integer> iterator() {
            return new Iterator<Integer>() {
                private IntIterator it = intIterator();

                public boolean hasNext() {
                    return it.isValid();
                }

                public Integer next() {
                    int result = it.value();
                    it.advance();
                    return result;
                }
            };
        }

        default public int compareTo(IntStream c) {
            IntIterator it = intIterator();
            IntIterator jt = c.intIterator();
            while (it.isValid() && jt.isValid()) {
                int i = it.value();
                int j = jt.value();
                if (i < j) {
                    return -1;
                } else if (i > j) {
                    return 1;
                }
                it.advance();
                jt.advance();
            }
            if (it.isValid()) {
                return 1;
            }
            if (jt.isValid()) {
                return -1;
            }
            return 0;
        }

    }

    static class Sorter {
        private static final int INSERTION_THRESHOLD = 16;

        private Sorter() {
        }

        public static void sort(IntList list, IntComparator comparator) {
            quickSort(list, 0, list.size() - 1, (Integer.bitCount(Integer.highestOneBit(list.size()) - 1) * 5) >> 1,
                    comparator);
        }

        private static void quickSort(IntList list, int from, int to, int remaining, IntComparator comparator) {
            if ((to - from) < INSERTION_THRESHOLD) {
                insertionSort(list, from, to, comparator);
                return;
            }
            if (remaining == 0) {
                heapSort(list, from, to, comparator);
                return;
            }
            remaining--;
            int pivotIndex = (from + to) >> 1;
            int pivot = list.get(pivotIndex);
            list.swap(pivotIndex, to);
            int storeIndex = from;
            int equalIndex = to;
            for (int i = from; i < equalIndex; i++) {
                int value = comparator.compare(list.get(i), pivot);
                if (value < 0) {
                    list.swap(storeIndex++, i);
                } else if (value == 0) {
                    list.swap(--equalIndex, i--);
                }
            }
            quickSort(list, from, storeIndex - 1, remaining, comparator);
            for (int i = equalIndex; i <= to; i++) {
                list.swap(storeIndex++, i);
            }
            quickSort(list, storeIndex, to, remaining, comparator);
        }

        private static void heapSort(IntList list, int from, int to, IntComparator comparator) {
            for (int i = ((to + from) - 1) >> 1; i >= from; i--) {
                siftDown(list, i, to, comparator, from);
            }
            for (int i = to; i > from; i--) {
                list.swap(from, i);
                siftDown(list, from, i - 1, comparator, from);
            }
        }

        private static void siftDown(IntList list, int start, int end, IntComparator comparator, int delta) {
            int value = list.get(start);
            while (true) {
                int child = ((start - delta) << 1) + 1 + delta;
                if (child > end) {
                    return;
                }
                int childValue = list.get(child);
                if ((child + 1) <= end) {
                    int otherValue = list.get(child + 1);
                    if (comparator.compare(otherValue, childValue) > 0) {
                        child++;
                        childValue = otherValue;
                    }
                }
                if (comparator.compare(value, childValue) >= 0) {
                    return;
                }
                list.swap(start, child);
                start = child;
            }
        }

        private static void insertionSort(IntList list, int from, int to, IntComparator comparator) {
            for (int i = from + 1; i <= to; i++) {
                int value = list.get(i);
                for (int j = i - 1; j >= from; j--) {
                    if (comparator.compare(list.get(j), value) <= 0) {
                        break;
                    }
                    list.swap(j, j + 1);
                }
            }
        }

    }

    static class Range {
        public static IntList range(int from, int to) {
            int[] result = new int[Math.abs(from - to)];
            int current = from;
            if (from <= to) {
                for (int i = 0; i < result.length; i++) {
                    result[i] = current++;
                }
            } else {
                for (int i = 0; i < result.length; i++) {
                    result[i] = current--;
                }
            }
            return new IntArray(result);
        }

    }

    static interface IntComparator {
        public int compare(int first, int second);

    }
}