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.util.Map;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.util.NavigableMap;
import java.io.UncheckedIOException;
import java.util.TreeMap;
import java.io.Closeable;
import java.io.Writer;
import java.util.Map.Entry;
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;
        TreeMap<Integer, Interval> map = new TreeMap<>();

        public void split(int l) {
            Interval floor = CollectionUtils.floorValue(map, 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 = CollectionUtils.ceilValue(map, 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 Item {
        int init;
        int cap;
        int speed;

    }

    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 Interval extends CloneSupportObject<Interval> {
        int l;
        int r;
        boolean dirty;
        int lastVisit;

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

    }

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

    }

    static class CollectionUtils {
        public static <K, V> V floorValue(NavigableMap<K, V> map, K key) {
            Map.Entry<K, V> entry = map.floorEntry(key);
            return entry == null ? null : entry.getValue();
        }

        public static <K, V> V ceilValue(NavigableMap<K, V> map, K key) {
            Map.Entry<K, V> entry = map.ceilingEntry(key);
            return entry == null ? null : entry.getValue();
        }

    }

    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 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();
        }

    }

    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;
        }

    }
}