// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
#include <numeric>
#include <cstdio>
#include <vector>
#include <bitset>
#include <deque>
#include <queue>
#include <cmath>
#include <ctime>
#include <set>
#include <map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define hashmap unordered_map
#define hashset unordered_set
#define heap priority_queue
#define MS(x, v, n) memset((x), (v), sizeof((x)[0]) * (n))
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef unsigned long long ull;
void file(const string FILE = "Test")
{
freopen((FILE + ".INP").c_str(), "r", stdin);
freopen((FILE + ".ERR").c_str(), "w", stderr);
freopen((FILE + ".OUT").c_str(), "w", stdout);
}
template<typename T> bool maxoreq(T &res, const T &val) { if (res <= val) { res = val; return true; } return false; }
template<typename T> bool minoreq(T &res, const T &val) { if (res >= val) { res = val; return true; } return false; }
template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val) { res = val; return true; } return false; }
const int mvx[] = {0, +1, +0, -0, -1, +1, +1, -1, -1, 0};
const int mvy[] = {0, +0, +1, -1, -0, +1, -1, -1, +1, 0};
/// ====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====
const db EPS = 1e-9;
const db PI = acos(-1);
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int LIM = 1e6 + 16;
struct Node
{
int length;
int open;
int close;
Node (int length = 0, int open = 0, int close = 0)
: length(length), open(open), close(close) {}
friend istream &operator >> (istream &cin, Node &a)
{
cin >> a.length >> a.open >> a.close;
return cin;
}
friend ostream &operator << (ostream &cout, const Node &a)
{
cout << a.length << " " << a.open << " " << a.close;
return cout;
}
};
Node operator + (const Node &a, const Node &b)
{
int extra = min(a.open, b.close);
Node c;
c.length = a.length + b.length + extra;
c.open = a.open + b.open - extra;
c.close = a.close + b.close - extra;
return c;
}
struct Segtree
{
int n;
vector<Node> t;
void init(int lim)
{
for (n = 1; n < lim; n <<= 1);
t.assign(n << 1, Node());
}
void modify(int p, char c, int ct, int lt, int rt)
{
if (rt - lt == 1)
{
t[ct] = (c == '(') ? Node(0, 1, 0) : Node(0, 0, 1);
return ;
}
int mt = (lt + rt) >> 1;
if (mt > p)
modify(p, c, ct * 2 + 1, lt, mt);
else
modify(p, c, ct * 2 + 2, mt, rt);
t[ct] = t[ct * 2 + 1] + t[ct * 2 + 2];
}
void modify(int p, char c)
{
return modify(p, c, 0, 0, n);
}
Node query(int l, int r, int ct, int lt, int rt)
{
if (lt >= r || l >= rt) return Node();
if (lt >= l && r >= rt) return t[ct];
int mt = (lt + rt) >> 1;
Node lv = query(l, r, ct * 2 + 1, lt, mt);
Node rv = query(l, r, ct * 2 + 2, mt, rt);
return lv + rv;
}
Node query(int l, int r)
{
return query(l, r + 1, 0, 0, n);
}
};
int main()
{
// file("Test");
ios::sync_with_stdio(NULL);
cin.tie(NULL);
string s;
cin >> s;
int n = s.size();
Segtree S;
S.init(n + 1);
for (int i = 1; i <= n; ++i)
S.modify(i, s[i - 1]);
int q;
cin >> q;
while (q-->0)
{
int l, r;
cin >> l >> r;
cout << 2 * S.query(l, r).length << "\n";
}
return 0;
}
Ly8gI3ByYWdtYSBHQ0Mgb3B0aW1pemUoIk8zLHVucm9sbC1sb29wcyIpCi8vICNwcmFnbWEgR0NDIHRhcmdldCgiYXZ4MixibWksYm1pMixsemNudCxwb3BjbnQiKQoKI2luY2x1ZGUgPHVub3JkZXJlZF9tYXA+CiNpbmNsdWRlIDx1bm9yZGVyZWRfc2V0PgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8aW9zdHJlYW0+CiN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IGM7Cn0KCnN0cnVjdCBTZWd0cmVlCnsKICAgIGludCBuOwogICAgdmVjdG9yPE5vZGU+IHQ7CgogICAgdm9pZCBpbml0KGludCBsaW0pCiAgICB7CiAgICAgICAgZm9yIChuID0gMTsgbiA8IGxpbTsgbiA8PD0gMSk7CiAgICAgICAgdC5hc3NpZ24obiA8PCAxLCBOb2RlKCkpOwogICAgfQoKICAgIHZvaWQgbW9kaWZ5KGludCBwLCBjaGFyIGMsIGludCBjdCwgaW50IGx0LCBpbnQgcnQpCiAgICB7CiAgICAgICAgaWYgKHJ0IC0gbHQgPT0gMSkKICAgICAgICB7CiAgICAgICAgICAgIHRbY3RdID0gKGMgPT0gJygnKSA/IE5vZGUoMCwgMSwgMCkgOiBOb2RlKDAsIDAsIDEpOwogICAgICAgICAgICByZXR1cm4gOwogICAgICAgIH0KCiAgICAgICAgaW50IG10ID0gKGx0ICsgcnQpID4+IDE7CiAgICAgICAgaWYgKG10ID4gcCkKICAgICAgICAgICAgbW9kaWZ5KHAsIGMsIGN0ICogMiArIDEsIGx0LCBtdCk7CiAgICAgICAgZWxzZQogICAgICAgICAgICBtb2RpZnkocCwgYywgY3QgKiAyICsgMiwgbXQsIHJ0KTsKCiAgICAgICAgdFtjdF0gPSB0W2N0ICogMiArIDFdICsgdFtjdCAqIDIgKyAyXTsKICAgIH0KCiAgICB2b2lkIG1vZGlmeShpbnQgcCwgY2hhciBjKQogICAgewogICAgICAgIHJldHVybiBtb2RpZnkocCwgYywgMCwgMCwgbik7CiAgICB9CgogICAgTm9kZSBxdWVyeShpbnQgbCwgaW50IHIsIGludCBjdCwgaW50IGx0LCBpbnQgcnQpCiAgICB7CiAgICAgICAgaWYgKGx0ID49IHIgfHwgbCA+PSBydCkgcmV0dXJuIE5vZGUoKTsKICAgICAgICBpZiAobHQgPj0gbCAmJiByID49IHJ0KSByZXR1cm4gdFtjdF07CgogICAgICAgIGludCBtdCA9IChsdCArIHJ0KSA+PiAxOwogICAgICAgIE5vZGUgbHYgPSBxdWVyeShsLCByLCBjdCAqIDIgKyAxLCBsdCwgbXQpOwogICAgICAgIE5vZGUgcnYgPSBxdWVyeShsLCByLCBjdCAqIDIgKyAyLCBtdCwgcnQpOwogICAgICAgIHJldHVybiBsdiArIHJ2OwogICAgfQoKICAgIE5vZGUgcXVlcnkoaW50IGwsIGludCByKQogICAgewogICAgICAgIHJldHVybiBxdWVyeShsLCByICsgMSwgMCwgMCwgbik7CiAgICB9Cn07CgppbnQgbWFpbigpCnsKLy8gICAgZmlsZSgiVGVzdCIpOwogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oTlVMTCk7CiAgICBjaW4udGllKE5VTEwpOwoKICAgIHN0cmluZyBzOwogICAgY2luID4+IHM7CgogICAgaW50IG4gPSBzLnNpemUoKTsKICAgIFNlZ3RyZWUgUzsKICAgIFMuaW5pdChuICsgMSk7CgogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgKytpKQogICAgICAgIFMubW9kaWZ5KGksIHNbaSAtIDFdKTsKCiAgICBpbnQgcTsKICAgIGNpbiA+PiBxOwoKICAgIHdoaWxlIChxLS0+MCkKICAgIHsKICAgICAgICBpbnQgbCwgcjsKICAgICAgICBjaW4gPj4gbCA+PiByOwogICAgICAgIGNvdXQgPDwgMiAqIFMucXVlcnkobCwgcikubGVuZ3RoIDw8ICJcbiI7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K