You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.

You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.

You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.

Input

The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.

Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.

It is guaranteed that the total number of monsters in all test cases does not exceed 300000.

Output

For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.

Example

Input


1
3
7 15
2 14
5 3


Output


6
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.stream.LongStream;
import java.util.Collection;
import java.util.function.IntConsumer;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.OptionalLong;
import java.util.List;
import java.util.stream.Stream;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC(in, out);
        try {
            solver.solve();
        } catch (ExitException ignored) {
        }
        out.close();
    }

    static class TaskC {
        FastScanner sc;
        PrintWriter out;

        public TaskC(FastScanner sc, PrintWriter out) {
            this.sc = sc;
            this.out = out;
        }

        public void solve() {
            times(sc.nextInt(), this::solve);
        }

        public void solve(int time) {
            int n = sc.nextInt();
            long[] a = new long[n];
            long[] b = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = sc.nextLong();
                b[i] = sc.nextLong();
            }

            long boo = 0;
            List<TaskC.Foo> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                if (a[i] > boo) {
                    list.add(new TaskC.Foo(i, boo, a[i]));
                }
                boo = b[i];
            }
            if (a[0] <= boo) {
                list.remove(0);
            } else {
                list.get(0).bonus = boo;
            }


            if (list.size() == 0) {
                answer(Arrays.stream(a).min().orElse(0));
            } else {
                TaskC.Foo foo = list.get(0);
                int from = foo.idx;
                long plus = foo.bonus;
                long ans = list.stream().mapToLong(f -> f.value - f.bonus).sum();
                for (int i = (from + 1) % n, li = 1 % list.size(); i != from; i = (i + 1) % n) {
                    foo = list.get(li);
                    if (foo.idx == i) {
                        plus = Math.min(plus, foo.bonus);
                        li = (li + 1) % list.size();
                    } else {
                        plus = Math.min(plus, a[i]);
                    }
                }
                answer(ans + plus);
            }
        }

        private void answer(Object ans) {
            out.println(ans);
            throw new ExitException();
        }

        private void times(int n, IntConsumer consumer) {
            for (int i = 0; i < n; i++) {
                try {
                    consumer.accept(i);
                } catch (ExitException ignored) {
                }
            }
        }

        static class Foo {
            int idx;
            long bonus;
            long value;

            public Foo(int idx, long bonus, long value) {
                this.idx = idx;
                this.bonus = bonus;
                this.value = value;
            }

            public String toString() {
                return "Foo[" + idx +
                        "] bonus=" + bonus +
                        ", value=" + value;
            }

        }

    }

    static class FastScanner {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

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

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

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

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

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

    }

    static class ExitException extends RuntimeException {
    }
}

