There are $$$n$$$ water tanks in a row, $$$i$$$-th of them contains $$$a_i$$$ liters of water. The tanks are numbered from $$$1$$$ to $$$n$$$ from left to right.You can perform the following operation: choose some subsegment $$$[l, r]$$$ ($$$1\le l \le r \le n$$$), and redistribute water in tanks $$$l, l+1, \dots, r$$$ evenly. In other words, replace each of $$$a_l, a_{l+1}, \dots, a_r$$$ by $$$\frac{a_l + a_{l+1} + \dots + a_r}{r-l+1}$$$. For example, if for volumes $$$[1, 3, 6, 7]$$$ you choose $$$l = 2, r = 3$$$, new volumes of water will be $$$[1, 4.5, 4.5, 7]$$$. You can perform this operation any number of times.What is the lexicographically smallest sequence of volumes of water that you can achieve?As a reminder:A sequence $$$a$$$ is lexicographically smaller than a sequence $$$b$$$ of the same length if and only if the following holds: in the first (leftmost) position where $$$a$$$ and $$$b$$$ differ, the sequence $$$a$$$ has a smaller element than the corresponding element in $$$b$$$.

The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — the number of water tanks. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — initial volumes of water in the water tanks, in liters. Because of large input, reading input as doubles is not recommended.

Print the lexicographically smallest sequence you can get. In the $$$i$$$-th line print the final volume of water in the $$$i$$$-th tank. Your answer is considered correct if the absolute or relative error of each $$$a_i$$$ does not exceed $$$10^{-9}$$$. Formally, let your answer be $$$a_1, a_2, \dots, a_n$$$, and the jury's answer be $$$b_1, b_2, \dots, b_n$$$. Your answer is accepted if and only if $$$\frac{|a_i - b_i|}{\max{(1, |b_i|)}} \le 10^{-9}$$$ for each $$$i$$$.

NoteIn the first sample, you can get the sequence by applying the operation for subsegment $$$[1, 3]$$$.In the second sample, you can't get any lexicographically smaller sequence.

'use strict'

const problem = (n, a) => {
    const w = [ [ a[0], 1 ] ];
    for (let i = 1; i < n; i++) {
        let x = a[i], y = 1;
        while (w.length && w[w.length - 1][0] * y > w[w.length - 1][1] * x) {
            const d = w.pop();
            x += d[0];
            y += d[1];
        }
        w.push([ x, y ]);
    }
    return w.map(d => { const x = d[0], y = d[1]; return `${x/y}\n`.repeat(y); }).join('');
}

print(problem(+readline(), readline().split(' ').map(i => +i)))


A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example:  bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)");  bracket sequences ")(", "(" and ")" are not. There was an RBS. Some brackets have been replaced with question marks. Is it true that there is a unique way to replace question marks with brackets, so that the resulting sequence is an RBS?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of testcases. The only line of each testcase contains an RBS with some brackets replaced with question marks. Each character is either '(', ')' or '?'. At least one RBS can be recovered from the given sequence. The total length of the sequences over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase, print "YES" if the way to replace question marks with brackets, so that the resulting sequence is an RBS, is unique. If there is more than one way, then print "NO".

NoteIn the first testcase, the only possible original RBS is "(())".In the second testcase, there are multiple ways to recover an RBS.In the third and the fourth testcases, the only possible original RBS is "()".In the fifth testcase, the original RBS can be either "((()()))" or "(())()()".

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const s = readLine();
        let result = myFunc(s) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(s){
    const n = s.length;
    if(n % 2 != 0) return false;

    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = n/2;
    for(let i = 0; i < n; i++){
        if(s[i] == '(') pos++;
        else if(s[i] == ')')neg++;
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;
    if(pos + neg + 2 < met) return false;

    //Generate main solution
    let aux_pos = 0, ref, acum = 0;
    let p = new Array(n);
    for(let i = 0; i < n; i++){
        if(s[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                p[i] = ++acum;
            }
            else{
                if(!ref) ref = i;
                p[i] = --acum;
            }
        }
        else{
            if(s[i] == '(') p[i] = ++acum;
            else p[i] = --acum;
        }
    }

    let min = new Array(n), atual_min = Infinity;
    for(let i = ref-1; i >= 0; i--){
        if(p[i] < atual_min) atual_min = p[i];
        min[i] = atual_min;
    }

    for(let i = 0; i < ref && inter_open > 0; i++){
        if(s[i] == '?'){

            if(min[i] - 2 >= 0) return false;
            inter_open--;
        }

    }
    return true;

}

'use strict';

function solve(s) {
  const n = s.length;
  if (n & 1) return 'NO';
  if (s[0] === ')' || s[n - 1] === '(') return 'NO';
  {
    let l = 0,
        r = 1,
        cnt = 0;
    for (let i = n - 2; i >= 0; i--) {
      if (i === 0) {
        l++;
      } else {
        if (s[i] === '(') l++;else if (s[i] === ')') r++;else cnt++;
      }
      if (l > r + cnt) return 'NO';
    }
    if (Math.abs(r - l) === cnt) return 'YES';
  }
  let l = 1,
      r = 0,
      cnt = 0,
      q = [];
  {
    for (let i = 1; i < n; i++) {
      if (i === n - 1) {
        r++;
      } else {
        if (s[i] === '(') l++;else if (s[i] === ')') r++;else {
          cnt++;
          q.push(i);
        }
      }
      if (r > l + cnt) return 'NO';
    }
  }
  const a = s.split('');
  a[0] = '(';
  a[n - 1] = ')';
  if (l < r) {
    for (let i = 0; i < r - l; i++) {
      a[q[i]] = '(';
    }
    q = q.slice(r - l);
  } else if (l > r) {
    let t = l - r;
    while (t--) {
      a[q.pop()] = ')';
    }
  }
  const m = q.length;
  for (let i = 0; i < m / 2 - 1; i++) {
    a[q[i]] = '(';
    a[q[m - i - 1]] = ')';
  }
  a[q[m / 2 - 1]] = ')';
  a[q[m / 2]] = '(';
  {
    let l = 0,
        r = 0;
    for (let i = 0; i < n; i++) {
      if (a[i] === '(') l++;else r++;
      if (r > l) return 'YES';
    }
  }
  return 'NO';
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    const val = {
        '?': 0,
        '(': 1,
        ')': -1
    };

    const isFixed = char => {
        return char === '(' || char === ')';
    };

    const validate = s => {
        let balance = 0;
        for (let i = 0; i < s.length; i++) {
            balance += val[s[i]];
            if (balance < 0) {
                return false;
            }
        }
        return balance === 0;
    };

    for (let test = 0; test < N; test++) {
        let s = readline();
        let balance = 0;
        let questions = 0;

        for (let i = 0; i < s.length; i++) {
            balance += val[s[i]];
            if (s[i] === '?') {
                questions++;
            }
        }

        let opening = (questions - balance) / 2;
        let closing = balance + opening;

        if (closing === 0 || opening === 0) {
            output('YES');
            continue;
        }

        let secondBest = '';

        let seq = '('.repeat(opening - 1) + ')' + '(' + ')'.repeat(closing - 1);

        let j = 0;
        for (let i = 0; i < s.length; i++) {
            if (isFixed(s[i])) {
                secondBest += s[i];
                continue;
            }
            secondBest += seq[j];
            j++;
        }

        const result = validate(secondBest) ? 'NO' : 'YES';

        output(result);
    }
}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const s = readLine();
        let result = myFunc(s) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(s){
    let arr;
    const n = s.length;
    if(n % 2 != 0) return false;

    function verify(pos, cont, ref){
        for(let i = pos; i <= ref; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = n/2;
    let esq = new Array(n), dir = new Array(n);
    let cont_esq = 0, cont_dir = 0;

    //Restrition for start and end
    let last = n-1;
    for(let i = 0; i < n; i++){
        if(s[i] == '('){
           esq[i] = ++cont_esq;
           pos++;
        }
        else if(s[i] == ')'){
            esq[i] = --cont_esq;
            neg++;
        }
        else esq[i] = cont_esq;

        if(s[last-i] == ')') dir[last-i] = ++cont_dir;
        else if(s[i] == '(') dir[last-i] = --cont_dir;
        else dir[last-i] = cont_dir;
    }

    // console.log(esq, dir);

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;
    if(pos + neg + 2 < met) return false;

    //count '?' and apply restrition
    let seq = 0;
    for(let i = 0; i < n; i++){
        if(s[i] == '?') seq++;
        else seq = 0;

        if(esq[i] >= 0 && dir[i] >= 0 && seq > Math.abs(esq[i] - dir[i]) + 2){
            return false;
        }
    }

    //Generate main solution
    let aux_pos = 0, ref;
    arr = new Array(n);
    for(let i = 0; i < n; i++){
        if(s[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';

            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }
        }
        else{
            arr[i] = s[i];
        }
    }

    // Try find other solution using swap
    let cont = 0;
    arr[ref] = '(';
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(s[i] == '?'){
            if(verify(i+1, cont-1, ref)) return false;
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const s = readLine();
        let result = myFunc(s) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(s){
    let arr;
    const n = s.length;
    if(n % 2 != 0) return false;

    function verify(pos, cont){
        for(let i = pos; i < n; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = n/2, vet = [];
    let esq = new Array(n), dir = new Array(n);
    let cont_esq = 0, cont_dir = 0;

    for(let i = 0; i < n; i++){
        if(s[i] == '(') pos++;
        else if(s[i] == ')') neg++;
        else vet.push(i);
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;
    if(vet.length - 2 > met) return false;

    //Restrition for start and end
    let last = n-1;
    for(let i = 0; i < n; i++){
        if(s[i] == '(') esq[i] = ++cont_esq;
        else if(s[i] == ')') esq[i] = --cont_esq;
        else esq[i] = cont_esq;

        if(s[last-i] == '(') dir[last-i] = ++cont_dir;
        else if(s[last-i] == ')') dir[last-i] = --cont_dir;
        else dir[last-i] = cont_dir;
    }

    // console.log(esq, dir);
    //count '?' and apply restrition
    let seq = 0;
    for(let i = 0; i < n; i++){
        if(s[i] == '?') seq++;
        else seq = 0;

        if(esq[i] >= 0 && dir[i] <= 0 && seq > Math.abs(esq[i] + dir[i]) + 3){
            return false;
        }

    }

    //Generate main solution
    let aux_pos = 0, ref;
    arr = new Array(n);
    for(let i = 0; i < n; i++){
        if(s[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }
        }
        else{
            arr[i] = s[i];
        }
    }

    // Try find other solution using swap
    let cont = 0;
    let ind = 0;
    arr[ref] = '(';
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const s = readLine();
        let result = myFunc(s) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(s){
    let arr;
    const n = s.length;
    if(n % 2 != 0) return false;

    function verify(pos, cont){
        for(let i = pos; i < n; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = n/2, vet = [];
    let esq = new Array(n), dir = new Array(n);
    let cont_esq = 0, cont_dir = 0;

    for(let i = 0; i < n; i++){
        if(s[i] == '(') pos++;
        else if(s[i] == ')') neg++;
        else vet.push(i);
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;
    if(vet.length - 2 > met) return false;

    //Restrition for start and end
    let last = n-1;
    for(let i = 0; i < n; i++){
        if(s[i] == '(') esq[i] = ++cont_esq;
        else if(s[i] == ')') esq[i] = --cont_esq;
        else esq[i] = cont_esq;

        if(s[last-i] == '(') dir[last-i] = ++cont_dir;
        else if(s[last-i] == ')') dir[last-i] = --cont_dir;
        else dir[last-i] = cont_dir;
    }

    //count '?' and apply restrition
    let seq = 0, aux1, aux2;
    for(let i = 0; i < n; i++){
        if(s[i] == '?') seq++;
        else seq = 0;

        aux1 = esq[i] >= 0 ? esq[i] : 0;
        aux2 = dir[i] <= 0 ? dir[i] : 0;
        if(seq > Math.abs(aux1 + aux2) + 2){
            return false;
        }

    }

    //Generate main solution
    let aux_pos = 0, ref;
    arr = new Array(n);
    for(let i = 0; i < n; i++){
        if(s[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }
        }
        else{
            arr[i] = s[i];
        }
    }

    // Try find other solution using swap
    let cont = 0;
    let ind = 0;
    arr[ref] = '(';
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const s = readLine();
        let result = myFunc(s) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(s){
    let arr;
    const n = s.length;
    if(n % 2 != 0) return false;

    function verify(pos, cont){
        for(let i = pos; i < n; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = n/2, vet = [];
    let esq = new Array(n), dir = new Array(n);
    let cont_esq = 0, cont_dir = 0;

    for(let i = 0; i < n; i++){
        if(s[i] == '(') pos++;
        else if(s[i] == ')') neg++;
        else vet.push(i);
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;
    if(vet.length - 2 > met) return false;

    //Restrition for start and end
    let last = n-1;
    for(let i = 0; i < n; i++){
        if(s[i] == '(') esq[i] = ++cont_esq;
        else if(s[i] == ')') esq[i] = --cont_esq;
        else esq[i] = cont_esq;

        if(s[last-i] == '(') dir[last-i] = ++cont_dir;
        else if(s[last-i] == ')') dir[last-i] = --cont_dir;
        else dir[last-i] = cont_dir;
    }

    //count '?' and apply restrition
    let seq = 0;
    for(let i = 0; i < n; i++){
        if(s[i] == '?') seq++;
        else seq = 0;

        // console.log(esq, dir);
        if((esq[i] > 0 || dir[i] < 0) && seq > Math.abs(esq[i] + dir[i]) + 2){
            return false;
        }

    }

    //Generate main solution
    let aux_pos = 0, ref;
    arr = new Array(n);
    for(let i = 0; i < n; i++){
        if(s[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }
        }
        else{
            arr[i] = s[i];
        }
    }

    // Try find other solution using swap
    let cont = 0;
    let ind = 0;
    arr[ref] = '(';
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const s = readLine();
        let result = myFunc(s) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(s){
    let arr;
    const n = s.length;
    if(n % 2 != 0) return false;

    function verify(pos, cont){
        for(let i = pos; i < n; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = n/2, vet = [];
    let esq = new Array(n), dir = new Array(n);
    let cont_esq = 0, cont_dir = 0;

    for(let i = 0; i < n; i++){
        if(s[i] == '(') pos++;
        else if(s[i] == ')') neg++;
        else vet.push(i);
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;
    if(vet.length - 2 > met) return false;

    //Restrition for start and end
    let last = n-1;
    for(let i = 0; i < n; i++){
        if(s[i] == '(') esq[i] = ++cont_esq;
        else if(s[i] == ')') esq[i] = --cont_esq;
        else esq[i] = cont_esq;

        if(s[last-i] == '(') dir[last-i] = --cont_dir;
        else if(s[last-i] == ')') dir[last-i] = ++cont_dir;
        else dir[last-i] = cont_dir;
    }

    //count '?' and apply restrition
    let seq = 0;
    for(let i = 0; i < n; i++){
        if(s[i] == '?') seq++;
        else seq = 0;
        if((esq[i] > 0 || dir[i] > 0) && seq > Math.max(esq[i], dir[i])){
            return false;
        }

    }

    //Generate main solution
    let aux_pos = 0, ref;
    arr = new Array(n);
    for(let i = 0; i < n; i++){
        if(s[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }
        }
        else{
            arr[i] = s[i];
        }
    }

    // Try find other solution using swap
    let cont = 0;
    let ind = 0;
    arr[ref] = '(';
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const s = readLine();
        let result = myFunc(s) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(s){
    let arr;
    const n = s.length;
    if(n % 2 != 0) return false;

    function verify(pos, cont){
        for(let i = pos; i < n; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = n/2, vet = [];
    let esq = new Array(n), dir = new Array(n);
    let cont_esq = 0, cont_dir = 0;

    for(let i = 0; i < n; i++){
        if(s[i] == '(') pos++;
        else if(s[i] == ')') neg++;
        else vet.push(i);
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;
    if(vet.length - 2 > met) return false;

    //Restrition for start and end
    let last = n-1;
    for(let i = 0; i < n; i++){
        if(s[i] == '(') esq[i] = ++cont_esq;
        else if(s[i] == ')') esq[i] = --cont_esq;
        else esq[i] = cont_esq;

        if(s[last-i] == '(') dir[last-i] = --cont_dir;
        else if(s[last-i] == ')') dir[last-i] = ++cont_dir;
        else dir[last-i] = cont_dir;
    }

    //count '?' and apply restrition
    let seq = 0;
    for(let i = 0; i < n; i++){
        if(s[i] == '?') seq++;
        else seq = 0;
        if((esq[i] != 0 || dir[i] != 0) && seq > Math.max(esq[i], dir[i])){
            return false;
        }

    }

    //Generate main solution
    let aux_pos = 0, ref;
    arr = new Array(n);
    for(let i = 0; i < n; i++){
        if(s[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }
        }
        else{
            arr[i] = s[i];
        }
    }

    // Try find other solution using swap
    let cont = 0;
    let ind = 0;
    arr[ref] = '(';
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const s = readLine();
        let result = myFunc(s) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(s){
    let arr;
    const n = s.length;
    if(n % 2 != 0) return false;

    function verify(pos, cont){
        for(let i = pos; i < n; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = n/2, vet = [];
    let esq = new Array(n), dir = new Array(n);
    let cont_esq = 0, cont_dir = 0;

    for(let i = 0; i < n; i++){
        if(s[i] == '(') pos++;
        else if(s[i] == ')') neg++;
        else vet.push(i);
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;
    if(vet.length - 2 > met) return false;

    //Restrition for start and end
    let last = n-1;
    for(let i = 0; i < n; i++){
        if(s[i] == '(') esq[i] = ++cont_esq;
        else if(s[i] == ')') esq[i] = --cont_esq;
        else esq[i] = cont_esq;

        if(s[last-i] == '(') dir[last-i] = ++cont_dir;
        else if(s[last-i] == ')') dir[last-i] = --cont_dir;
        else dir[last-i] = cont_dir;
    }

    //count '?' and apply restrition
    let seq = 0;
    for(let i = 0; i < n; i++){
        if(s[i] == '?') seq++;
        else seq = 0;
        if(esq[i] != 0 && dir[i] != 0 && seq >= Math.abs(esq[i] + dir[i]) + 2){
            return false;
        }

    }

    //Generate main solution
    let aux_pos = 0, ref;
    arr = new Array(n);
    for(let i = 0; i < n; i++){
        if(s[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }
        }
        else{
            arr[i] = s[i];
        }
    }

    // Try find other solution using swap
    let cont = 0;
    let ind = 0;
    arr[ref] = '(';
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        // const arr = readLine().split(''); //o fato de usar split quebrou o desempenho
        const s = readLine();
        let result = myFunc(s) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(s){
    function verify(pos, cont){
        for(let i = pos; i < s.length; i++){
            if(cont < 0) return false;
            if(s[i] == '(') cont++;
            else if(s[i] == ')') cont--;
        }
        return true;
    }

    if(s.length %2 != 0) return false;
    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = s.length/2;
    let vet = [];

    for(let i = 0; i < s.length; i++){
        if(s[i] == '(') pos++;
        else if(s[i] == ')') neg++;
        else vet.push(i);
        // if(vet.length - 2 > met) return false;
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;

    // if(vet.length - 2 > met) return false;

    let arr = new Array(s.length);
    let aux_pos = 0, ref;
    for(let i = 0; i < s.length; i++){
        if(s[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }
        }
        else{
            arr[i] = s[i];
        }
    }

    let cont = 0;
    let ind = 0;
    arr[ref] = '(';

    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        // const arr = readLine().split(''); //o fato de usar split quebrou o desempenho
        const s = readLine();
        let result = myFunc(s) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(s){
    function verify(pos, cont){
        for(let i = pos; i < s.length; i++){
            if(cont < 0) return false;
            if(s[i] == '(') cont++;
            else if(s[i] == ')') cont--;
        }
        return true;
    }

    if(s.length %2 != 0) return false;
    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = s.length/2;
    let vet = [];

    for(let i = 0; i < s.length; i++){
        if(s[i] == '(') pos++;
        else if(s[i] == ')') neg++;
        else vet.push(i);
        // if(vet.length - 2 > met) return false;
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;

    if(vet.length - 2 > met) return false;

    let arr = new Array(s.length);
    let aux_pos = 0, ref;
    for(let i = 0; i < s.length; i++){
        if(s[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }
        }
        else{
            arr[i] = s[i];
        }
    }

    let cont = 0;
    let ind = 0;
    arr[ref] = '(';

    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        // const arr = readLine().split(''); //o fato de usar split quebrou o desempenho
        const s = readLine();
        let result = myFunc(s) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(s){
    function verify(pos, cont){
        for(let i = pos; i < s.length; i++){
            if(cont < 0) return false;
            if(s[i] == '(') cont++;
            else if(s[i] == ')') cont--;
        }
        return true;
    }

    if(s.length %2 != 0) return false;
    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = s.length/2;
    let vet = [];

    for(let i = 0; i < s.length; i++){
        if(s[i] == '(') pos++;
        else if(s[i] == ')') neg++;
        else vet.push(i);
        // if(vet.length - 2 > met) return false;
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;

    if(vet.length - pos - neg - 2 > 0 ) return false;

    let arr = new Array(s.length);
    let aux_pos = 0, ref;
    for(let i = 0; i < s.length; i++){
        if(s[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }
        }
        else{
            arr[i] = s[i];
        }
    }

    let cont = 0;
    let ind = 0;
    arr[ref] = '(';

    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        // const arr = readLine().split('');
        const arr = readLine();
        let result = myFunc(arr) ? 'YES' : 'NO';
        console.log(result);
    }
}

String.prototype.replaceAt = function (index, newValue) {
    return this.substring(0, index) + newValue + this.substring(index + newValue.length);
};

function myFunc(arr){
    function verify(pos, cont){
        for(let i = pos; i < arr.length; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    if(arr.length %2 != 0) return false;
    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = arr.length/2;
    let vet = [];

    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '(') pos++;
        else if(arr[i] == ')') neg++;
        else vet.push(i);
        // if(vet.length - 2 > met) return false;
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;

    if(vet.length - pos - neg - 2 > 0 ) return false;

    let aux_pos = 0, ref;
    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr = arr.replaceAt(i, '(');
                // arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                // arr[i] = ')';
                arr = arr.replaceAt(i, ')');
            }
        }
    }

    let cont = 0;
    let ind = 0;
    // arr[ref] = '(';
    console.log(arr);
    arr = arr.replaceAt(ref, '(');
    console.log(arr);
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        // const arr = readLine().split('');
        const arr = readLine();
        let result = myFunc(arr) ? 'YES' : 'NO';
        console.log(result);
    }
}

String.prototype.replaceAt = function (index, newValue) {
    this.substring(0, index) + newValue + this.substring(index + newValue.length);
};

function myFunc(arr){
    function verify(pos, cont){
        for(let i = pos; i < arr.length; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    if(arr.length %2 != 0) return false;
    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = arr.length/2;
    let vet = [];

    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '(') pos++;
        else if(arr[i] == ')') neg++;
        else vet.push(i);
        // if(vet.length - 2 > met) return false;
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;

    if(vet.length - pos - neg - 2 > 0 ) return false;

    let aux_pos = 0, ref;
    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr.replaceAt(i, '(');
                // arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                // arr[i] = ')';
                arr.replaceAt(i, ')');
            }
        }
    }

    let cont = 0;
    let ind = 0;
    // arr[ref] = '(';
    arr.replaceAt(ref, '(');
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const arr = readLine().split('');
        let result = myFunc(arr) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(arr){
    function verify(pos, cont){
        for(let i = pos; i < arr.length; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    if(arr.length %2 != 0) return false;
    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = arr.length/2;
    let vet = [];

    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '(') pos++;
        else if(arr[i] == ')') neg++;
        else vet.push(i);
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;
    if(vet.length - inter_open + inter_close - 2 > 0 ) return false;

    let aux_pos = 0, ref;
    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }
        }
    }

    let cont = 0;
    let ind = 0;
    arr[ref] = '(';
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const arr = readLine().split('');
        let result = myFunc(arr) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(arr){
    function verify(pos, cont){
        for(let i = pos; i < arr.length; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    if(arr.length %2 != 0) return false;
    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = arr.length/2;
    let vet = [];

    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '(') pos++;
        else if(arr[i] == ')') neg++;
        else vet.push(i);
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;
    if(vet.length - Math.abs(inter_open - inter_close) - 2 > 0 ) return false;

    let aux_pos = 0, ref;
    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }

        }
    }

    // console.log(arr, inter_open, inter_close);

    let cont = 0;
    let ind = 0;
    arr[ref] = '(';
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == vet[ind]){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const arr = readLine().split('');
        let result = myFunc(arr) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(arr){
    function verify(pos, cont){
        for(let i = pos; i < arr.length; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
        }
        return true;
    }

    if(arr.length %2 != 0) return false;
    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = arr.length/2;
    let vet = [];

    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '(') pos++;
        else if(arr[i] == ')') neg++;
        else vet.push(i);
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;

    let aux_pos = 0, ref;
    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '?'){
            if(aux_pos < inter_open){
                aux_pos++;
                arr[i] = '(';
            }
            else{
                if(!ref) ref = i;
                arr[i] = ')';
            }

        }
    }

    let cont = 0;
    let ind = 0;
    arr[ref] = '(';
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(i == ind){
            if(verify(i+1, cont-1)) return false;
            ind++
            inter_open--;
            cont++;
        }
        else{
            if(arr[i] == '(') cont++;
            else cont--;
        }
    }
    return true;

}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const arr = readLine().split('');
        let result = myFunc(arr) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(arr){

    function verify(pos, cont){
        for(let i = pos; i < arr.length; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
            // else return true;
        }
        return true;
    }

    if(arr.length %2 != 0) return false;
    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = arr.length/2;

    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '(') pos++;
        else if(arr[i] == ')') neg++
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;

    let cont = 0;
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(arr[i] == '(') cont++;
        else if(arr[i] == ')') cont--;
        else{
            let res = verify(i+1, cont-1);
            if(res) return false;
            inter_open--;
            cont++;
        }
    }
    return true;

}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){
        const arr = readLine().split('');
        let result = myFunc(arr) ? 'YES' : 'NO';
        console.log(result);
    }
}

function myFunc(arr){

    function verify(pos, cont){
        for(let i = pos; i < arr.length; i++){
            if(cont < 0) return false;
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')') cont--;
            else return true;
        }
    }

    if(arr.length %2 != 0) return false;
    let pos = 0, neg = 0, inter_open = 0, inter_close = 0;
    let met = arr.length/2;

    for(let i = 0; i < arr.length; i++){
        if(arr[i] == '(') pos++;
        else if(arr[i] == ')') neg++
    }

    if(pos > met || neg > met) return false;
    inter_open = met - pos;
    inter_close = met - neg;
    if(inter_open == 0 || inter_close == 0) return true;

    let cont = 0;
    for(let i = 0; i < arr.length && inter_open > 0; i++){
        if(arr[i] == '(') cont++;
        else if(arr[i] == ')') cont--;
        else{
            let res = verify(i+1, cont-1);
            if(res) return false;
            inter_open--;
            cont++;
        }
    }
    return true;

}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){

        const arr = readLine().split('');
        let result = myFunc(arr);
        console.log(result);
    }
}

function myFunc(arr){
    if(arr.length %2 != 0) return 'NO';
    let unique = true;
    let dyn = new Array(arr.length).fill().map(() => new Array());

    function rec(pos, cont, opt){
        if(!unique) return false;
        if(cont < 0) return false;
        for(let i = pos; i < arr.length; i++){
            if(arr[i] == '('){
               cont++;
               opt = 0;
            }
            else if(arr[i] == ')'){
                if(cont == 0){
                    dyn[i][cont] = false;
                    return false;
                }
                opt = 0;
                cont--;
            }
            else{// if is ?
                if(opt > 2){
                    dyn[i][cont] = false;
                    unique = false;
                    return false;
                }
                let op1 = (i >= arr.length-1) ? cont+1 == 0 : rec(i+1, cont+1, opt+1);
                let op2 = (i >= arr.length-1) ? cont-1 == 0 : rec(i+1, cont-1, opt+1);
                // console.log(i, op1, op2, cont, unique);
                if(op1 && op2 && unique){
                    dyn[i][cont] = false;
                    unique = false;
                    return false;
                }
                dyn[i][cont] = op1 || op2;
                return op1 || op2;
            }
        }
        dyn[pos][cont] = (cont == 0);
        return cont == 0;
    }

    return rec(0, 0, 0) && unique ? 'YES' : 'NO';

}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){

        const arr = readLine().split('');
        let result = myFunc(arr);
        console.log(result);
    }
}

function myFunc(arr){
    if(arr.length %2 != 0) return 'NO';
    let unique = true;
    let dyn = new Array(arr.length).fill().map(() => new Array());

    function rec(pos, cont, opt){
        if(!unique) return false;
        if(cont < 0) return false;
        for(let i = pos; i < arr.length; i++){
            if(arr[i] == '('){
               cont++;
               opt = 0;
            }
            else if(arr[i] == ')'){
                if(cont == 0){
                    dyn[i][cont] = false;
                    return false;
                }
                opt = 0;
                cont--;
            }
            else{// if is ?
                if(opt > 1){
                    dyn[i][cont] = false;
                    unique = false;
                    return false;
                }
                let op1 = (i >= arr.length-1) ? cont+1 == 0 : rec(i+1, cont+1, opt+1);
                let op2 = (i >= arr.length-1) ? cont-1 == 0 : rec(i+1, cont-1, opt+1);
                // console.log(i, op1, op2, cont, unique);
                if(op1 && op2 && unique){
                    dyn[i][cont] = false;
                    unique = false;
                    return false;
                }
                dyn[i][cont] = op1 || op2;
                return op1 || op2;
            }
        }
        dyn[pos][cont] = (cont == 0);
        return cont == 0;
    }

    return rec(0, 0, 0) && unique ? 'YES' : 'NO';

}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){

        const arr = readLine().split('');
        let result = myFunc(arr);
        console.log(result);
    }
}

function myFunc(arr){
    if(arr.length %2 != 0) return 'NO';
    let unique = true;
    let dyn = new Array(arr.length).fill().map(() => new Array(arr.length));
    function rec(arr, pos, cont){
        if(dyn[pos][cont] != undefined) return dyn[pos][cont];
        if(!unique) return false;
        for(let i = pos; i < arr.length; i++){
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')'){
                if(cont == 0){
                    dyn[i][cont] = false;
                    return false;
                }
                cont--;
            }
            else{// if ?
                let new_arr = [...arr];
                new_arr[i] = '(';
                let op1 = rec(new_arr, i, cont);
                new_arr[i] = ')';
                let op2 = rec(new_arr, i, cont);
                // console.log(i, op1, op2, arr, cont, unique);
                if(op1 && op2 && unique){
                    dyn[i][cont] = false;
                    unique = false;
                    return false;
                }
                dyn[i][cont] = op1 || op2;
                return op1 || op2;
            }
        }
        dyn[pos][cont] = (cont == 0);
        return cont == 0;
    }

    return rec(arr, 0, 0) && unique ? 'YES' : 'NO';

}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '', currentLine = 0;
process.stdin.on('data', inputStdin => { inputString += inputStdin; });
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    const t = parseInt(readLine(), 10);
    for(let i = 0; i < t; i++){

        const arr = readLine().split('');
        let result = myFunc(arr);
        console.log(result);
    }
}

function myFunc(arr){
    if(arr.length %2 != 0) return 'NO';
    let unique = true;
    let dyn = new Array(arr.length).fill().map(() => new Array(arr.length));
    function rec(arr, pos, cont){
        if(dyn[pos][cont] != undefined) return dyn[pos][cont];
        if(!unique) return false;
        for(let i = pos; i < arr.length; i++){
            if(arr[i] == '(') cont++;
            else if(arr[i] == ')'){
                if(cont == 0) return false;
                cont--;
            }
            else{// if ?
                let new_arr = [...arr];
                new_arr[i] = '(';
                let op1 = rec(new_arr, i, cont);
                new_arr[i] = ')';
                let op2 = rec(new_arr, i, cont);
                // console.log(i, op1, op2, arr, cont, unique);
                if(op1 && op2 && unique){
                    dyn[pos][cont] = false;
                    unique = false;
                    return false;
                }
                dyn[pos][cont] = op1 || op2;
                return op1 || op2;
            }
        }
        dyn[pos][cont] = (cont == 0);
        return cont == 0;
    }

    return rec(arr, 0, 0) && unique ? 'YES' : 'NO';

}

'use strict';

function solve(s) {
  const n = s.length;
  if (n & 1) return 'NO';
  if (s[0] === ')' || s[n - 1] === '(') return 'NO';
  {
    let l = 0,
        r = 1,
        cnt = 0;
    for (let i = n - 2; i >= 0; i--) {
      if (i === 0) {
        l++;
      } else {
        if (s[i] === '(') l++;else if (s[i] === ')') r++;else cnt++;
      }
      if (l > r + cnt) return 'NO';
    }
    if (Math.abs(r - l) === cnt) return 'YES';
  }
  let l = 1,
      r = 0,
      cnt = 0,
      q = [];
  {
    for (let i = 1; i < n; i++) {
      if (i === n - 1) {
        r++;
      } else {
        if (s[i] === '(') l++;else if (s[i] === ')') r++;else {
          cnt++;
          q.push(i);
        }
      }
      if (r > l + cnt) return 'NO';
    }
  }
  const a = s.split('');
  if (l < r) {
    for (let i = 0; i < r - l; i++) {
      a[q[i]] = '(';
    }
    q = q.slice(r - l);
  } else if (l > r) {
    let t = l - r;
    while (t--) {
      a[q.pop()] = ')';
    }
  }
  const m = q.length;
  for (let i = 0; i < m / 2 - 1; i++) {
    a[q[i]] = '(';
    a[q[m - i - 1]] = ')';
  }
  a[q[m / 2 - 1]] = ')';
  a[q[m / 2]] = '(';
  {
    let l = 0,
        r = 0;
    for (let i = 0; i < n; i++) {
      if (a[i] === '(') l++;else r++;
      if (r > l) return 'YES';
    }
  }
  return 'NO';
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(s) {
  const n = s.length;
  if (n & 1) return 'NO';
  if (s[0] === ')' || s[n - 1] === '(') return 'NO';
  {
    let l = 1,
        r = 0,
        cnt = 0;
    for (let i = 1; i < n; i++) {
      if (i === n - 1) {
        r++;
      } else {
        if (s[i] === '(') l++;else if (s[i] === ')') r++;else cnt++;
      }
      if (r > l + cnt) return 'NO';
    }
    if (l + cnt === r) return 'YES';
    if (r + cnt === l) return 'YES';
    if (cnt === 1) return 'YES';
  }
  const a = s.split('');
  a[0] = '(';
  a[n - 1] = ')';
  {
    let l = 0,
        r = 0,
        q = [];
    let j = 0;
    for (let i = 0; i < n; i++) {
      if (a[i] === '(') l++;else if (a[i] === ')') r++;else {
        if (q.length === j && l === r) {
          a[i] = '(';
          l++;
        } else {
          q.push(i);
        }
      }
      if (r > l) {
        if (j === q.length) return 'NO';
        a[q[j++]] = '(';
        l++;
      }
      if (i === n - 1) {
        while (l > r) {
          if (j === q.length) return 'NO';
          a[q[j++]] = ')';
          r++;
        }
      }
    }
  }
  {
    let l = 0,
        r = 0,
        q = [];
    let j = 0;
    for (let i = n - 1; i >= 0; i--) {
      if (a[i] === '(') l++;else if (a[i] === ')') r++;else {
        if (q.length === j && l === r) {
          a[i] = ')';
          r++;
        } else {
          q.push(i);
        }
      }
      if (r < l) {
        if (j === q.length) return 'NO';
        a[q[j++]] = ')';
        r++;
      }
      if (i === 0) {
        while (l < r) {
          if (j === q.length) return 'NO';
          a[q[j++]] = '(';
          l++;
        }
      }
    }
  }
  for (let i = 0; i < n; i++) {
    if (a[i] === '?') return 'NO';
  }
  return 'YES';
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(s) {
  const n = s.length;
  if (n & 1) return 'NO';
  if (s[0] === ')' || s[n - 1] === '(') return 'NO';
  {
    let l = 1,
        r = 0,
        cnt = 0;
    for (let i = 1; i < n; i++) {
      if (i === n - 1) {
        r++;
      } else {
        if (s[i] === '(') l++;else if (s[i] === ')') r++;else cnt++;
      }
      if (r > l + cnt) return 'NO';
    }
    if (l + cnt === r) return 'YES';
    if (r + cnt === l) return 'YES';
    if (cnt === 1) return 'YES';
  }
  const a = s.split('');
  a[0] = '(';
  a[n - 1] = ')';
  {
    let l = 0,
        r = 0,
        q = [];
    let j = 0;
    for (let i = 0; i < n; i++) {
      if (a[i] === '(') l++;else if (a[i] === ')') r++;else {
        if (q.length === j && l === r) {
          a[i] = '(';
          l++;
        } else {
          q.push(i);
        }
      }
      if (r > l) {
        if (j === q.length) return 'NO';
        a[q[j++]] = '(';
        l++;
      }
      if (i === n - 1) {
        while (l > r) {
          if (j === q.length) return 'NO';
          a[q[j++]] = ')';
          r++;
        }
      }
    }
    for (let i = 0; i < n; i++) {
      if (a[i] === '?') return 'NO';
    }
    return 'YES';
  }
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(s) {
  const n = s.length;
  if (n & 1) return 'NO';
  if (s[0] === ')' || s[n - 1] === '(') return 'NO';
  {
    let l = 1,
        r = 0,
        cnt = 0;
    for (let i = 1; i < n; i++) {
      if (i === n - 1) {
        r++;
      } else {
        if (s[i] === '(') l++;else if (s[i] === ')') r++;else cnt++;
      }
      if (r > l + cnt) return 'NO';
    }
    if (l + cnt === r) return 'YES';
    if (r + cnt === l) return 'YES';
    if (cnt === 1) return 'YES';
  }
  const a = s.split('');
  a[0] = '(';
  a[n - 1] = ')';
  {
    let l = 0,
        r = 0,
        q = [];
    let j = 0;
    for (let i = 0; i < n; i++) {
      if (a[i] === '(') l++;else if (a[i] === ')') r++;else {
        if (!q.length && l === r) {
          a[i] = '(';
          l++;
        } else {
          q.push(i);
        }
      }
      if (r > l) {
        if (j === q.length) return 'NO';
        a[q[j++]] = '(';
        l++;
      }
      if (i === n - 1) {
        while (l > r) {
          if (j === q.length) return 'NO';
          a[q[j++]] = ')';
          r++;
        }
      }
    }
    for (let i = 0; i < n; i++) {
      if (a[i] === '?') return 'NO';
    }
    return 'YES';
  }
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(s) {
  const n = s.length;
  if (n & 1) return 'NO';
  if (s[0] === ')' || s[n - 1] === '(') return 'NO';
  {
    let l = 1,
        r = 0,
        cnt = 0;
    for (let i = 1; i < n; i++) {
      if (i === n - 1) {
        r++;
      } else {
        if (s[i] === '(') l++;else if (s[i] === ')') r++;else cnt++;
      }
      if (r > l + cnt) return 'NO';
    }
    if (l + cnt === r) return 'YES';
    if (r + cnt === l) return 'YES';
    if (cnt === 1) return 'YES';
  }
  const a = s.split('');
  a[0] = '(';
  a[n - 1] = ')';
  {
    let l = 0,
        r = 0,
        q = [];
    let j = 0;
    for (let i = 0; i < n; i++) {
      if (a[i] === '(') l++;else if (a[i] === ')') r++;else {
        q.push(i);
      }
      if (r > l) {
        if (j === q.length) return 'NO';
        a[q[j++]] = '(';
        l++;
      }
      if (i === n - 1) {
        while (l > r) {
          if (j === q.length) return 'NO';
          a[q[j++]] = ')';
          r++;
        }
      }
    }
    for (let i = 0; i < n; i++) {
      if (a[i] === '?') return 'NO';
    }
    return 'YES';
  }
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(s) {
  const n = s.length;
  if (n & 1) return 'NO';
  if (s[0] === ')' || s[n - 1] === '(') return 'NO';
  {
    let l = 0,
        r = 0,
        cnt = 0;
    for (let i = 1; i < n - 1; i++) {
      if (s[i] === '(') l++;else if (s[i] === ')') r++;else cnt++;
      if (r > l + cnt) return 'NO';
    }
    if (l + cnt === r) return 'YES';
    if (r + cnt === l) return 'YES';
    if (cnt === 1) return 'YES';
  }
  let l = [1],
      r = [];
  r[n - 1] = 1;
  for (let i = 1; i < n; i++) {
    l[i] = l[i - 1];
    if (s[i] === '(') l[i] = l[i - 1] + 1;
  }
  for (let i = n - 2; i >= 0; i--) {
    r[i] = r[i + 1];
    if (s[i] === ')') r[i]++;
  }
  let left = false,
      right = false;
  for (let i = 1; i < n - 1; i++) {
    if (s[i] === '?') {
      if (!left && !right) {
        if (l[i] === r[i]) return 'NO';
        if (l[i] > r[i]) {
          right = true;
        } else if (l[i] < r[i]) {
          left = true;
        }
      } else if (right) {
        if (l[i] <= r[i]) return 'NO';
      } else {
        if (r[i] <= l[i]) return 'NO';
      }
    }
  }
  return 'YES';
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(s) {
  const n = s.length;
  if (n & 1) return 'NO';
  if (s[0] === ')' || s[n - 1] === '(') return 'NO';
  {
    let l = 0,
        r = 0,
        cnt = 0;
    for (let i = 1; i < n - 1; i++) {
      if (s[i] === '(') l++;else if (s[i] === ')') r++;else cnt++;
      if (r > l + cnt) return 'NO';
    }
    if (cnt === 1) return 'YES';
  }
  let l = [1],
      r = [];
  r[n - 1] = 1;
  for (let i = 1; i < n; i++) {
    l[i] = l[i - 1];
    if (s[i] === '(') l[i] = l[i - 1] + 1;
  }
  for (let i = n - 2; i >= 0; i--) {
    r[i] = r[i + 1];
    if (s[i] === ')') r[i]++;
  }
  let left = false,
      right = false;
  for (let i = 1; i < n - 1; i++) {
    if (s[i] === '?') {
      if (!left && !right) {
        if (l[i] === r[i]) return 'NO';
        if (l[i] > r[i]) {
          right = true;
        } else {
          left = true;
        }
      } else if (right) {
        if (l[i] <= r[i]) return 'NO';
      } else {
        if (r[i] <= l[i]) return 'NO';
      }
    }
  }
  return 'YES';
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(s) {
  const arr = [1, 1, 0, 0];
  if (s.length & 1) return 'NO';
  if (s[0] === ')' || s[s.length - 1] === '(') return 'NO';
  for (let i = 1; i < s.length - 1; i++) {
    if (s[i] === '(') {
      arr[0]++;
      arr[1]++;
    } else if (s[i] === ')') {
      arr[2]++;
      arr[3]++;
    } else {
      arr[1]++;
      arr[3]++;
    }
    if (arr[2] > arr[1]) return 'NO';
  }
  arr[2]++;
  arr[3]++;
  const dif = arr[1] - arr[0];
  if (arr[0] === arr[3] || arr[1] === arr[2]) return 'YES';
  if (arr[0] === arr[1] && dif) return 'NO';
  if (Math.abs(arr[2] - arr[0]) === dif) return 'YES';
  return 'NO';
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(s) {
  const arr = [1, 1, 0, 0];
  if (s[0] === ')' || s[s.length - 1] === '(') return 'NO';
  for (let i = 1; i < s.length - 1; i++) {
    if (s[i] === '(') {
      arr[0]++;
      arr[1]++;
    } else if (s[i] === ')') {
      arr[2]++;
      arr[3]++;
    } else {
      arr[1]++;
      arr[3]++;
    }
    if (arr[2] > arr[1]) return 'NO';
  }
  arr[2]++;
  arr[3]++;
  const dif = arr[1] - arr[0];
  if (dif > 1) return 'NO';
  return arr[0] === arr[3] || arr[1] === arr[2] ? 'YES' : 'NO';
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(s) {
  const arr = [1, 1, 0, 0];
  if (s[0] === ')' || s[s.length - 1] === '(') return 'NO';
  for (let i = 1; i < s.length - 1; i++) {
    if (s[i] === '(') {
      arr[0]++;
      arr[1]++;
    } else if (s[i] === ')') {
      arr[2]++;
      arr[3]++;
    } else {
      arr[1]++;
      arr[3]++;
    }
    if (arr[2] > arr[1]) return 'NO';
  }
  return arr[0] === arr[3] + 1 || arr[1] === arr[2] + 1 ? 'YES' : 'NO';
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(s) {
  const arr = [1, 1, 0, 0];
  if (s[0] === ')' || s[s.length - 1] === '(') return 'NO';
  for (let i = 1; i < s.length; i++) {
    if (i === s.length - 1) {
      arr[2]++;
      arr[3]++;
    } else {
      if (s[i] === '(') {
        arr[0]++;
        arr[1]++;
      } else if (s[i] === ')') {
        arr[2]++;
        arr[3]++;
      } else {
        arr[1]++;
        arr[3]++;
      }
    }
    if (arr[2] > arr[1]) return 'NO';
  }
  return arr[1] === arr[2] ? 'YES' : 'NO';
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const s = await read();
      res[i] = solve(s);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    const val = {
        '?': 0,
        '(': 1,
        ')': -1
    };

    const isFixed = char => {
        return char === '(' || char === ')';
    };

    const validate = s => {
        let balance = 0;
        for (let i = 0; i < s.length; i++) {
            balance += val[s[i]];
            if (balance < 0) {
                return false;
            }
        }
        return balance === 0;
    };

    for (let test = 0; test < N; test++) {
        let s = readline();
        if (test === 74) {
            console.log(s);
            return;
        }

        let balance = 0;
        let questions = 0;

        for (let i = 0; i < s.length; i++) {
            balance += val[s[i]];
            if (s[i] === '?') {
                questions++;
            }
        }

        let opening = (questions - balance) / 2;
        let closing = balance + opening;

        if (opening < 2 && closing < 2) {
            output('YES');
            continue;
        }

        let secondBest = '';
        let earlyClosingPlaced = false;
        let lateOpeningPlaced = false;

        for (let i = 0; i < s.length; i++) {
            if (isFixed(s[i])) {
                secondBest += s[i];
                continue;
            }
            if (opening > 1) {
                secondBest += '(';
                opening--;
            } else if (opening === 1 && !earlyClosingPlaced) {
                secondBest += ')';
                closing--;
                earlyClosingPlaced = true;
            } else if (opening === 1 && earlyClosingPlaced && !lateOpeningPlaced) {
                secondBest += '(';
                opening--;
                lateOpeningPlaced = true;
            } else {
                secondBest += ')';
            }
        }

        const result = validate(secondBest) ? 'NO' : 'YES';

        output(result);
    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    const val = {
        '?': 0,
        '(': 1,
        ')': -1
    };

    const isFixed = char => {
        return char === '(' || char === ')';
    };

    const validate = s => {
        let balance = 0;
        for (let i = 0; i < s.length; i++) {
            balance += val[s[i]];
            if (balance < 0) {
                return false;
            }
        }
        return balance === 0;
    };

    for (let test = 0; test < N; test++) {
        let s = readline();

        let balance = 0;
        let questions = 0;

        for (let i = 0; i < s.length; i++) {
            balance += val[s[i]];
            if (s[i] === '?') {
                questions++;
            }
        }

        let opening = (questions - balance) / 2;
        let closing = balance + opening;

        if (opening < 2 && closing < 2) {
            output('YES');
            continue;
        }

        let secondBest = '';
        let earlyClosingPlaced = false;
        let lateOpeningPlaced = false;

        for (let i = 0; i < s.length; i++) {
            if (isFixed(s[i])) {
                secondBest += s[i];
                continue;
            }
            if (opening > 1) {
                secondBest += '(';
                opening--;
            } else if (opening === 1 && !earlyClosingPlaced) {
                secondBest += ')';
                closing--;
                earlyClosingPlaced = true;
            } else if (opening === 1 && earlyClosingPlaced && !lateOpeningPlaced) {
                secondBest += '(';
                opening--;
                lateOpeningPlaced = true;
            } else {
                secondBest += ')';
            }
        }

        const result = validate(secondBest) ? 'NO' : 'YES';

        output(result);
    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    const trim = s => {
        let check = true;
        while (check) {
            const idx = s.split('').findIndex(
                (x, i) => i < s.length && x === '(' && s[i + 1] === ')'
            );
            if (idx === -1) {
                check = false;
            } else {
                s = s.replace('()', '');
            }
        }
        return s;
    };

    const val = {
        '?': 0,
        '(': 1,
        ')': -1
    };

    const validate = s => {
        let balance = 0;
        for (let i = 0; i < s.length; i++) {
            balance += val[s[i]];
            if (balance < 0) {
                return false;
            }
        }
        return balance === 0;
    };

    const countSolutions = s => {
        let nextQ = s.indexOf('?');
        if (nextQ === -1) {
            return validate(s) ? 1 : 0;
        }
        let lvl = 0;
        for (let i = 0; i < nextQ; i++) {
            if (lvl < 0) {
                return 0;
            }
            lvl += val[s[i]];
        }
        if (lvl > 0) {
            return countSolutions(s.replace('?', ')')) + countSolutions(s.replace('?', '('));
        } else {
            return countSolutions(s.replace('?', '('));
        }
    }

    for (let test = 0; test < N; test++) {
        let s = readline();
        s = trim(s);

        let total = 0;

        let questions = s.split('').filter(x => x === '?').length;
        if (questions > 6) {
            output('NO');
            continue;
        }

        const countSolutions = s => {
            if (total > 1) {
                return 0;
            }
            let nextQ = s.indexOf('?');
            if (nextQ === -1) {
                let correct = validate(s);
                if (correct) {
                    total++;
                }
                return correct ? 1 : 0;
            }
            let lvl = 0;
            for (let i = 0; i < nextQ; i++) {
                if (lvl < 0) {
                    return 0;
                }
                lvl += val[s[i]];
            }
            if (lvl > 0) {
                return countSolutions(s.replace('?', ')')) + countSolutions(s.replace('?', '('));
            } else {
                return countSolutions(s.replace('?', '('));
            }
        }

        let sol = countSolutions(s);
        let result = 'YES';
        if (sol > 1) {
            result = 'NO';
        }

        output(result);
    }
}