Problem: Lord Tirek is a centaur and the main antagonist in the season four finale episodes in the series "My Little Pony: Friendship Is Magic". In "Twilight's Kingdom" (Part 1), Tirek escapes from Tartarus and drains magic from ponies to grow stronger.

<image>

The core skill of Tirek is called Absorb Mana. It takes all mana from a magic creature and gives them to the caster.

Now to simplify the problem, assume you have n ponies (numbered from 1 to n). Each pony has three attributes:

  * si : amount of mana that the pony has at time 0; 
  * mi : maximum mana that the pony can have; 
  * ri : mana regeneration per unit time. 



Lord Tirek will do m instructions, each of them can be described with three integers: ti, li, ri. The instruction means that at time ti, Tirek will use Absorb Mana on ponies with numbers from li to ri (both borders inclusive). We'll give you all the m instructions in order, count how much mana Tirek absorbs for each instruction.

Input

The first line contains an integer n (1 ≤ n ≤ 105) — the number of ponies. Each of the next n lines contains three integers si, mi, ri (0 ≤ si ≤ mi ≤ 105; 0 ≤ ri ≤ 105), describing a pony. 

The next line contains an integer m (1 ≤ m ≤ 105) — the number of instructions. Each of the next m lines contains three integers ti, li, ri (0 ≤ ti ≤ 109; 1 ≤ li ≤ ri ≤ n), describing an instruction of Lord Tirek. The instructions are given in strictly increasing order of ti (all ti are distinct).

Output

For each instruction, output a single line which contains a single integer, the total mana absorbed in this instruction.

Examples

Input

5
0 10 1
0 12 1
0 20 1
0 12 1
0 10 1
2
5 1 5
19 1 5


Output

25
58

Note

Every pony starts with zero mana. For the first instruction, each pony has 5 mana, so you get 25 mana in total and each pony has 0 mana after the first instruction.

For the second instruction, pony 3 has 14 mana and other ponies have mana equal to their mi.

Solution (in JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.function.IntConsumer;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            ELittlePonyAndLordTirek solver = new ELittlePonyAndLordTirek();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class ELittlePonyAndLordTirek {
        int lim = (int) 1e5;
        VanEmdeBoasTreeMap<Interval> map = new VanEmdeBoasTreeMap<>((int) 1e5 + 1);

        public void split(int l) {
            Interval floor = map.floorValue(l);
            if (floor != null && floor.r >= l) {
                Interval right = floor.clone();
                right.l = l;
                floor.r = l - 1;
                map.put(l, right);
            }
        }

        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.ri();
            Item[] items = new Item[n + 1];

            NoTagPersistentSegment[] st = new NoTagPersistentSegment[n + 1];
            st[0] = NoTagPersistentSegment.NIL;

            for (int i = 1; i <= n; i++) {
                items[i] = new Item();
                items[i].init = in.ri();
                items[i].cap = in.ri();
                items[i].speed = in.ri();

                st[i] = st[i - 1].clone();
                if (items[i].speed != 0) {
                    int time = DigitUtils.ceilDiv(items[i].cap, items[i].speed);
                    st[i].update(time, time, 0, lim, items[i].cap, items[i].speed);
                }
            }

            for (int i = 1; i <= n; i++) {
                Interval interval = new Interval();
                interval.dirty = true;
                interval.l = interval.r = i;
                interval.lastVisit = 0;
                map.put(interval.l, interval);
            }

            int m = in.ri();
            for (int i = 0; i < m; i++) {
                int t = in.ri();
                int l = in.ri();
                int r = in.ri();
                split(l);
                split(r + 1);

                Interval head = new Interval();
                head.l = l;
                head.r = r;
                head.lastVisit = t;
                head.dirty = false;

                long sum = 0;
                while (true) {
                    Interval interval = map.ceilValue(l);
                    if (interval == null || interval.l > r) {
                        break;
                    }
                    map.remove(interval.l);

                    if (interval.dirty) {
                        Item item = items[interval.l];
                        sum += Math.min(item.init + item.speed * (long) t, item.cap);
                    } else {
                        int delta = t - interval.lastVisit;
                        sum += (st[interval.r].queryCap(0, delta, 0, lim) - st[interval.l - 1].queryCap(0, delta, 0, lim)) +
                                (st[interval.r].querySpeed(delta + 1, lim, 0, lim) - st[interval.l - 1].querySpeed(delta + 1, lim, 0, lim)) * delta;
                    }
                }

                map.put(head.l, head);
                out.println(sum);
            }
        }

    }

    static class Interval extends CloneSupportObject<Interval> {
        int l;
        int r;
        boolean dirty;
        int lastVisit;

        public String toString() {
            return String.format("(%d,%d)", l, r);
        }

    }

    static class VanEmdeBoasTreeMap<T> {
        private VanEmdeBoasTreeBeta tree;
        private Object[] data;
        private int u;

        public VanEmdeBoasTreeMap(int n) {
            this.u = n;
            data = new Object[n];
            tree = VanEmdeBoasTreeBeta.newInstance(n);
        }

        private boolean tooSmall(int i) {
            return i < 0;
        }

        private boolean tooLarge(int i) {
            return i >= u;
        }

        public void put(int key, T val) {
            assert !tooSmall(key) && !tooLarge(key);
            if (!tree.member(key)) {
                tree.insert(key);
            }
            data[key] = val;
        }

        public T remove(int key) {
            if (tooLarge(key) || tooSmall(key)) {
                return null;
            }
            if (tree.member(key)) {
                tree.delete(key);
                return (T) data[key];
            }
            return null;
        }

        public T floorValue(int key) {
            key = floorKey(key);
            if (key == -1) {
                return null;
            }
            return (T) data[key];
        }

        public int floorKey(int key) {
            if (tooSmall(key)) {
                return -1;
            }
            if (tooLarge(key)) {
                key = u - 1;
            }
            if (tree.member(key)) {
                return key;
            }
            return tree.predecessor(key);
        }

        public T ceilValue(int key) {
            key = ceilKey(key);
            if (key == -1) {
                return null;
            }
            return (T) data[key];
        }

        public int ceilKey(int key) {
            if (tooLarge(key)) {
                return -1;
            }
            if (tooSmall(key)) {
                key = 0;
            }
            if (tree.member(key)) {
                return key;
            }
            return tree.successor(key);
        }

        public String toString() {
            StringBuilder ans = new StringBuilder("{");
            tree.visit(i -> {
                ans.append(i).append(':').append(data[i]).append(',');
            });
            if (ans.length() > 1) {
                ans.setLength(ans.length() - 1);
            }
            ans.append('}');
            return ans.toString();
        }

    }

    static class NoTagPersistentSegment implements Cloneable {
        public static final NoTagPersistentSegment NIL = new NoTagPersistentSegment();
        private NoTagPersistentSegment left;
        private NoTagPersistentSegment right;
        private long sumOfCap;
        private long sumOfSpeed;

        static {
            NIL.left = NIL.right = NIL;
        }

        public void pushUp() {
            sumOfCap = left.sumOfCap + right.sumOfCap;
            sumOfSpeed = left.sumOfSpeed + right.sumOfSpeed;
        }

        public NoTagPersistentSegment() {
            left = right = NIL;
        }

        private boolean covered(int ll, int rr, int l, int r) {
            return ll <= l && rr >= r;
        }

        private boolean noIntersection(int ll, int rr, int l, int r) {
            return ll > r || rr < l;
        }

        public void update(int ll, int rr, int l, int r, int cap, int speed) {
            if (covered(ll, rr, l, r)) {
                sumOfSpeed += speed;
                sumOfCap += cap;
                return;
            }
            int m = DigitUtils.floorAverage(l, r);
            if (!noIntersection(ll, rr, l, m)) {
                left = left.clone();
                left.update(ll, rr, l, m, cap, speed);
            }
            if (!noIntersection(ll, rr, m + 1, r)) {
                right = right.clone();
                right.update(ll, rr, m + 1, r, cap, speed);
            }
            pushUp();
        }

        public long queryCap(int ll, int rr, int l, int r) {
            if (this == NIL || noIntersection(ll, rr, l, r)) {
                return 0;
            }
            if (covered(ll, rr, l, r)) {
                return sumOfCap;
            }
            int m = DigitUtils.floorAverage(l, r);
            return left.queryCap(ll, rr, l, m) +
                    right.queryCap(ll, rr, m + 1, r);
        }

        public long querySpeed(int ll, int rr, int l, int r) {
            if (this == NIL || noIntersection(ll, rr, l, r)) {
                return 0;
            }
            if (covered(ll, rr, l, r)) {
                return sumOfSpeed;
            }
            int m = DigitUtils.floorAverage(l, r);
            return left.querySpeed(ll, rr, l, m) +
                    right.querySpeed(ll, rr, m + 1, r);
        }

        public NoTagPersistentSegment clone() {
            try {
                return (NoTagPersistentSegment) super.clone();
            } catch (CloneNotSupportedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    static class Item {
        int init;
        int cap;
        int speed;

    }

    static class FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int ri() {
            return readInt();
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

    }

    static class VanEmdeBoasTreeBeta {
        final static int NIL = -1;
        int[] cluster;
        int[] start;
        int[] summary;
        int[] min;
        int[] max;
        byte[] b;
        byte[] h;
        int clusterTail;
        int nodeTail;
        int nodeCnt;
        int clusterCnt;

        private int allocNode() {
            return nodeTail++;
        }

        private int allocCluster(int len) {
            int ans = clusterTail;
            clusterTail += len;
            assert clusterTail <= start.length;
            return ans;
        }

        int high(int V, int i) {
            return i >> b[V];
        }

        int low(int V, int i) {
            return i & ((1 << b[V]) - 1);
        }

        int index(int V, int i, int j) {
            return (i << b[V]) + j;
        }

        int clusterLength(int V) {
            return 1 << h[V];
        }

        public void visit(IntConsumer consumer) {
            visit(0, consumer, 0);
        }

        private void visit(int V, IntConsumer consumer, int offset) {
            if (min[V] != NIL) {
                consumer.accept(min[V] + offset);
            } else {
                return;
            }
            if (summary[V] == NIL) {
                if (max[V] != NIL && max[V] != min[V]) {
                    consumer.accept(max[V] + offset);
                }
                return;
            }

            for (int i = 0; i < clusterLength(V); i++) {
                visit(cluster[i + start[V]], consumer, offset + (i << b[V]));
            }
        }

        public String toString() {
            StringBuilder ans = new StringBuilder("{");
            visit(0, i -> ans.append(i).append(','), 0);
            if (ans.length() > 1) {
                ans.setLength(ans.length() - 1);
            }
            ans.append("}");
            return ans.toString();
        }

        public static VanEmdeBoasTreeBeta newInstance(int u) {
            return new VanEmdeBoasTreeBeta(Log2.ceilLog(u));
        }

        private void estimate(int log) {
            nodeCnt++;
            if (log <= 1) {
                return;
            }
            int b = (log / 2);
            int len = 1 << (log - b);
            clusterCnt += len;
            //memory_used += cluster.length;
            for (int i = 0; i < len; i++) {
                estimate(b);
            }
            estimate(log - b);
        }

        private int build(int log) {
            int V = allocNode();
            if (log <= 1) {
                return V;
            }
            b[V] = (byte) (log / 2);
            h[V] = (byte) (log - b[V]);
            int len = 1 << h[V];
            start[V] = allocCluster(len);
            //memory_used += cluster.length;
            for (int i = 0; i < len; i++) {
                cluster[start[V] + i] = build(b[V]);
            }
            summary[V] = build(h[V]);
            return V;
        }

        private VanEmdeBoasTreeBeta(int log) {
            estimate(log);
            cluster = new int[clusterCnt];
            start = new int[nodeCnt];
            summary = new int[nodeCnt];
            min = new int[nodeCnt];
            max = new int[nodeCnt];
            b = new byte[nodeCnt];
            h = new byte[nodeCnt];
            Arrays.fill(min, -1);
            Arrays.fill(max, -1);
            Arrays.fill(summary, -1);

            build(log);
            assert clusterTail == cluster.length;
            assert nodeTail == start.length;
        }

        public boolean member(int x) {
            return member(0, x);
        }

        private boolean member(int V, int x) {
            if (x == min[V] || x == max[V]) {
                return true;
            } else if (summary[V] == NIL) {
                return false;
            }
            return member(cluster[high(V, x) + start[V]], low(V, x));
        }

        public int successor(int x) {
            return successor(0, x);
        }

        private int successor(int V, int x) {
            if (summary[V] == NIL) {
                if (x == 0 && max[V] == 1) {
                    return 1;
                }
                return NIL;
            } else if (min[V] != NIL && x < min[V]) {
                return min[V];
            } else {
                int maxlow = max[cluster[high(V, x) + start[V]]];
                if (maxlow != NIL && low(V, x) < maxlow) {
                    int offset = successor(cluster[high(V, x) + start[V]], low(V, x));
                    return index(V, high(V, x), offset);
                } else {
                    int succcluster = successor(summary[V], high(V, x));
                    if (succcluster == NIL) {
                        return NIL;
                    } else {
                        int offset = min[cluster[succcluster + start[V]]];
                        return index(V, succcluster, offset);
                    }
                }
            }
        }

        public int predecessor(int x) {
            return predecessor(0, x);
        }

        public int predecessor(int V, int x) {
            if (summary[V] == NIL) {
                if (x == 1 && min[V] == 0) {
                    return 0;
                }
                return NIL;
            } else if (max[V] != NIL && x > max[V]) {
                return max[V];
            } else {
                int minlow = min[cluster[high(V, x) + start[V]]];
                if (minlow != NIL && low(V, x) > minlow) {
                    int offset = predecessor(cluster[high(V, x) + start[V]], low(V, x));
                    return index(V, high(V, x), offset);
                } else {
                    int predcluster = predecessor(summary[V], high(V, x));
                    if (predcluster == NIL) {
                        if (min[V] != NIL && x > min[V]) {
                            return min[V];
                        } else {
                            return NIL;
                        }
                    } else {
                        int offset = max[cluster[predcluster + start[V]]];
                        return index(V, predcluster, offset);
                    }
                }
            }
        }

        void emptyTreeInsert(int V, int x) {
            min[V] = max[V] = x;
        }

        public void insert(int x) {
            insert(0, x);
        }

        private void insert(int V, int x) {
            if (min[V] == NIL) {
                emptyTreeInsert(V, x);
            } else {
                if (x < min[V]) {
                    int tmp = x;
                    x = min[V];
                    min[V] = tmp;
                }
                if (summary[V] != NIL) {
                    if (min[cluster[high(V, x) + start[V]]] == NIL) {
                        insert(summary[V], high(V, x));
                        emptyTreeInsert(cluster[high(V, x) + start[V]], low(V, x));
                    } else {
                        insert(cluster[high(V, x) + start[V]], low(V, x));
                    }
                }
                if (x > max[V]) {
                    max[V] = x;
                }
            }
        }

        public void delete(int x) {
            delete(0, x);
        }

        private void delete(int V, int x) {
            if (min[V] == max[V]) {
                min[V] = max[V] = NIL;
            } else if (summary[V] == NIL) {
                if (x == 0) {
                    min[V] = 1;
                } else {
                    min[V] = 0;
                }
                max[V] = min[V];
            } else {
                if (x == min[V]) {
                    int firstcluster = min[summary[V]];
                    x = index(V, firstcluster, min[cluster[firstcluster + start[V]]]);
                    min[V] = x;
                }
                delete(cluster[high(V, x) + start[V]], low(V, x));
                if (min[cluster[high(V, x) + start[V]]] == NIL) {
                    delete(summary[V], high(V, x));
                    if (x == max[V]) {
                        int summarymax = max[summary[V]];
                        if (summarymax == NIL) {
                            max[V] = min[V];
                        } else {
                            max[V] = index(V, summarymax, max[cluster[summarymax + start[V]]]);
                        }
                    }
                } else if (x == max[V]) {
                    max[V] = index(V, high(V, x), max[cluster[high(V, x) + start[V]]]);
                }
            }
        }

    }

    static class DigitUtils {
        private DigitUtils() {
        }

        public static int floorDiv(int a, int b) {
            return a < 0 ? -ceilDiv(-a, b) : a / b;
        }

        public static int ceilDiv(int a, int b) {
            if (a < 0) {
                return -floorDiv(-a, b);
            }
            int c = a / b;
            if (c * b < a) {
                return c + 1;
            }
            return c;
        }

        public static int floorAverage(int x, int y) {
            return (x & y) + ((x ^ y) >> 1);
        }

    }

    static class Log2 {
        public static int ceilLog(int x) {
            if (x <= 0) {
                return 0;
            }
            return 32 - Integer.numberOfLeadingZeros(x - 1);
        }

    }

    static abstract class CloneSupportObject<T> implements Cloneable {
        public T clone() {
            try {
                return (T) super.clone();
            } catch (CloneNotSupportedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private static final int THRESHOLD = 32 << 10;
        private final Writer os;
        private StringBuilder cache = new StringBuilder(THRESHOLD * 2);

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        private void afterWrite() {
            if (cache.length() < THRESHOLD) {
                return;
            }
            flush();
        }

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput append(char c) {
            cache.append(c);
            afterWrite();
            return this;
        }

        public FastOutput append(long c) {
            cache.append(c);
            afterWrite();
            return this;
        }

        public FastOutput append(String c) {
            cache.append(c);
            afterWrite();
            return this;
        }

        public FastOutput println(long c) {
            return append(c).println();
        }

        public FastOutput println() {
            return append(System.lineSeparator());
        }

        public FastOutput flush() {
            try {
//            boolean success = false;
//            if (stringBuilderValueField != null) {
//                try {
//                    char[] value = (char[]) stringBuilderValueField.get(cache);
//                    os.write(value, 0, cache.length());
//                    success = true;
//                } catch (Exception e) {
//                }
//            }
//            if (!success) {
                os.append(cache);
//            }
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }
}