'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){
    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 = '', 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);
    }
}


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

let standardInputString = '';
let currentLine = 0;

process.stdin.on('data', (rawData) => {
  standardInputString += rawData;
});

process.stdin.on('end', () => {
  standardInputString = standardInputString
    .trim()
    .split('\n')
    .map((line) => {
      return line
        .split(' ')
        .map((char) =>
          Number(char) > -Infinity && Number(char) < Infinity
            ? Number(char)
            : char
        );
    });
  solveProblem();
});

const readLine = () => standardInputString[currentLine++];

const solveProblem = function () {
  const t = readLine()[0];
  for (let i = 0; i < t; i++) {
    const [x, n] = readLine();
    if (n % 4 === 0) console.log(x);
    else if ((n - 1) % 4 === 0) console.log(x % 2 === 0 ? x - n : x + n);
    else if ((n - 2) % 4 === 0) console.log(x % 2 === 0 ? x + 1 : x - 1);
    else if ((n - 3) % 4 === 0)
      console.log(x % 2 === 0 ? x + 1 + n : x - 1 - n);
  }
};

'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++];
}
/* let arr = readLine().split(" ").map((x) => {return parseInt(x)});
let test = readLine();
while(test--)
{

}
*/
function main() {
    let test = readLine();
while(test--)
{
    let arr = readLine().split(" ").map((x) => {return parseInt(x)});
    let x0 = arr[0];
    let n = arr[1];
    if (x0%2 == 0){
        if (n%4 == 0){
            console.log(x0);
        }
        else if (n%4==1){
            console.log(x0-n);
        }
        else if (n%4 == 2){
            console.log(x0+1);
        }
        else if (n%4 == 3){
            console.log(x0+1+n);
        }

    }
    else{
        if (n%4 == 0){
            console.log(x0);
        }
        else if (n%4==1){
            console.log(x0+n);
        }
        else if (n%4 == 2){
            console.log(x0-1);
        }
        else if (n%4 == 3){
            console.log(x0-1-n);
        }
    }

}
    return 0;
}

"use strict";
exports.__esModule = true;
var readline = require("readline");
var process = require("process");
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});
var lines = [];
rl.on("line", function (line) {
    lines.push(line);
});
rl.on("close", function () {
    main();
});
function main() {
    for (var i = 1; i < lines.length; i++) {
        var fields = lines[i].split(" ");
        var x0 = parseInt(fields[0]);
        var n = parseInt(fields[1]);
        console.log(solve(x0, n));
    }
}
function jump(x, i) {
    if (x % 2 != 0) {
        x += i;
    }
    else {
        x -= i;
    }
    return x;
}
function solve(x0, n) {
    var i = 1;
    if (i < n) {
        i += Math.floor((n - i) / 4) * 4;
    }
    while (i <= n) {
        x0 = jump(x0, i);
        i++;
    }
    return x0;
}

'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 main() {
    let t = Number(readline())
    for (let j = 1; j <= t; j++) {
        let [x,n]=readline().split(' ').map(Number);
        if(n==0)
        {
            console.log(x);
            continue;
        }
        let i = 1;
        let ans=0;
        if(n%4==1)
            ans=-n;
        else if(n%4==2)
            ans=1;
        else if(n%4==3)
            ans=n+1;
        if(Math.abs(x)%2==1)
            ans=-ans;
        console.log(ans+x);
    }
}

'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 gcd(a, b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
// let a=readline().split(' ').map(Number)
// a.sort((a,b)=>a-b)

function main() {
    let t = Number(readline())
    for (let j = 1; j <= t; j++) {
        let [x,n]=readline().split(' ').map(Number);
        if(n==0)
        {
            console.log(x);
            continue;
        }
        let i = 1;
        let ans=0;
        if(n%4==1)
            ans=-n;
        else if(n%4==2)
            ans=1;
        else if(n%4==3)
            ans=n+1;
        if(Math.abs(x)%2==1)
            ans=-ans;
        console.log(ans+x);
    }
}

const solveProblem = function (problem) {
  for (let i = 0; i < problem.T; i++) {
    const [x, n] = problem.testCases[i];
    if (n % 4 === 0) {
      console.log(x);
    } else if ((n - 1) % 4 === 0) {
      console.log(x % 2 === 0 ? x - n : x + n);
    } else if ((n - 2) % 4 === 0) {
      console.log(x % 2 === 0 ? x + 1 : x - 1);
    } else if ((n - 3) % 4 === 0) {
      console.log(x % 2 === 0 ? x + 1 + n : x - 1 - n);
    }
  }
};

// -4 -3 -2 -1 0 1 2 3 4 5 6 7
//f

function readInput() {
  const readline = require('readline');
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false,
  });

  const problem = {
    T: 0,
    testCases: [],
    // testCase1: [],
    // testCase2: [],
  };

  let cursor = 1;

  rl.on('line', function (line) {
    // TODO: Process input
    if (cursor === 1) {
      // Get number of test cases from first line
      problem.T = +line;
      cursor++;
    } else {
      const [input1, input2] = line.split(' ');
      const input = [];

      input.push(+input1);
      input.push(+input2);
      problem.testCases.push(input);
      cursor++;
    }
  }).on('close', () => {
    // Finished processing input, now solve question
    solveProblem(problem);
    process.exit();
  });
}

readInput();

// cat input.txt | node app.js // Terminal command to run the code

process.stdin.setEncoding('utf-8')
const readline = require('readline')

const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    }
)

let count = -1
let inputArr = []

rl.on('line', data => {
    if (count === -1) {
        count = +data
    } else if (count !== 0) {
        inputArr.push(data.trim())
        count--
    }
    // max number 2^(53) - 1 ~~ 10^(15)
    if (count === 0) {
        inputArr = inputArr.map(i => i.split(' ').map(j => BigInt(j)))
        main()
        process.exit(0)
    }
})

function solve([a, n]) {
    let sum = a
    for (let i = 1n; i <= n; i++) {
        sum += (sum % 2n === 0n ? -i : i)
        console.log(i,' ',sum)
    }
    console.log('s',sum)
}

function solve1([a, n]) {
    if (a % 2n === 0n) {
        switch (n % 4n) {
            case 1n: {
                console.log((-n + a).toString())
                break;
            }
            case 2n: {
                console.log((1n + a).toString())
                break;
            }
            case 3n: {
                console.log((n + 1n + a).toString())
                break;
            }
            case 0n: {
                console.log((a).toString())
                break;
            }
        }
    } else {
        switch (n % 4n) {
            case 1n: {
                console.log((n + a).toString())
                break;
            }
            case 2n: {
                console.log((a - 1n).toString())
                break;
            }
            case 3n: {
                console.log((-n + a - 1n).toString())
                break;
            }
            case 0n: {
                console.log((a).toString())
                break;
            }
        }
    }
}

function main() {
    for (let i = 0; i < inputArr.length; i++) solve1(inputArr[i])
}

"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");

function print(x) {
  console.log(x);
}
let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

function main(){
  let input = Number(readline().replace('\r',''));
  let output = [];
  for(let i=0; i<input; i++){
    let point = readline().replace('\r','').split(' ');
    output.push(position(Number(point[0]), Number(point[1])))
  }
  for(let j=0; j<output.length; j++){
    print(output[j])
  }
}

function position(x, jump){
  let j = jump - 1;
  if(x%2===0){

  if(j%4===1) {
  x = x + Math.floor(jump/4)*(-4) + (-1) + jump;
  }

  else if(j%4===2){
  x = x + (jump-1)*2 + Math.floor(jump/4)*(-4);
  }

  else if(j%4===3) {
  x = x;
  }

  else if(j%4===0){
  x = x + Math.floor(jump/4)*(-4) + (-1);
  }

  }

  else{

  if(j%4===1) {
  x = x + Math.floor(jump/4)*(4) + (1) + (-1)*jump;
  }

  else if(j%4===2){
  x = x + (jump-1)*(-2) + Math.floor(jump/4)*(4);
  }

  else if(j%4===3) {
  x = x;
  }

  else if(j%4===0){
  x = x + Math.floor(jump/4)*(4) + (1);
  }

  }

  return x;
}

var dataitr;
var stdin_input = "";
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { dataitr = stdin_input.split('\n').values(); main(); });

const rl = () => dataitr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

function main () {
	let T = rn();
	while (T--) {
		const [x0, n] = rna();

		const k = n % 4;

		let ans = 0;
		if (k == 1) {
			ans = -n;
		} else if (k == 2) {
			ans = 1;
		} else if (k == 3) {
			ans = n + 1;
		}

		if (x0&1) ans = -ans;

		ans += x0;

		console.log(ans);
	}
}

var dataitr;
var stdin_input = "";
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { dataitr = stdin_input.split('\n').values(); main(); });

const rl = () => dataitr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

function main () {
	let t = rn();
	while (t--) {
		let [x, n] = rna();

		let anss = [0, -n, 1, n + 1];

		let ans = (x % 2 ? -1 : 1) * anss[n % 4];

		console.log(x + ans);
	}
}

let stop = '';
process.stdin.on('data', c => stop += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = stop.split(EOL);
    var t = lines[0];
    for(i = 1; i <= t; i++){
        var k = lines[i].split(' ').map(Number);
        var x = k[0], n = k[1];
        var v = [0, -n, 1, n + 1];
        var ans = (x % 2 ? -1 : 1) * v[n % 4];
        console.log(x + ans);
    }
});

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

let standardInputString = '';
let currentLine = 0;

process.stdin.on('data', (rawData) => {
  standardInputString += rawData;
});

process.stdin.on('end', () => {
  standardInputString = standardInputString
    .trim()
    .split('\n')
    .map((line) => {
      return line
        .split(' ')
        .map((char) =>
          Number(char) > -Infinity && Number(char) < Infinity
            ? Number(char)
            : char
        );
    });
  solveProblem();
});

const readLine = () => standardInputString[currentLine++];

const solveProblem = function () {
  const t = readLine()[0];
  console.log(t);
  for (let i = 0; i < t; i++) {
    const [x, n] = readLine();
    if (n % 4 === 0) console.log(x);
    else if ((n - 1) % 4 === 0) console.log(x % 2 === 0 ? x - n : x + n);
    else if ((n - 2) % 4 === 0) console.log(x % 2 === 0 ? x + 1 : x - 1);
    else if ((n - 3) % 4 === 0)
      console.log(x % 2 === 0 ? x + 1 + n : x - 1 - n);
  }
};

process.stdin.setEncoding('utf-8')
const readline = require('readline')

const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    }
)

let count = -1
let inputArr = []

rl.on('line', data => {
    if (count === -1) {
        count = +data
    } else if (count !== 0) {
        inputArr.push(data.trim())
        count--
    }
    // max number 2^(53) - 1 ~~ 10^(15)
    if (count === 0) {
        inputArr = inputArr.map(i => i.split(' ').map(j => BigInt(j)))
        main()
        process.exit(0)
    }
})

function solve([a, n]) {
    // let sum = a
    // for (let i = 1; i <= b; i++) {
    //
    //     sum += (sum % 2 === 0 ? -i : i)
    //     console.log(i,' ',sum)
    // }
    // console.log('s',sum)
    if (a % 2n === 0n) {
        switch (n % 4n) {
            case 1n: {
                console.log((-n+a).toString())
                break;
            }
            case 2n: {
                console.log((1n+a).toString())
                break;
            }
            case 3n: {
                console.log((n+1n+a).toString())
                break;
            }
            case 0n: {
                console.log((a).toString())
                break;
            }
        }
    } else {
        switch (n % 4n) {
            case 1n: {
                console.log((n+a).toString())
                break;
            }
            case 2n: {
                console.log((n-1n).toString())
                break;
            }
            case 3n: {
                console.log((-n+a-1n).toString())
                break;
            }
            case 0n: {
                console.log((a).toString())
                break;
            }
        }

    }
}

function main() {
    for (let i = 0; i < inputArr.length; i++) solve(inputArr[i])
}

process.stdin.setEncoding('utf-8')
const readline = require('readline')

const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    }
)

let count = -1
let inputArr = []

rl.on('line', data => {
    if (count === -1) {
        count = +data
    } else if (count !== 0) {
        inputArr.push(data.trim())
        count--
    }
    // max number 2^(53) - 1 ~~ 10^(15)
    if (count === 0) {
        inputArr = inputArr.map(i => i.split(' ').map(j => +j))
        main()
        process.exit(0)
    }
})

function solve([a, n]) {
    // let sum = a
    // for (let i = 1; i <= b; i++) {
    //
    //     sum += (sum % 2 === 0 ? -i : i)
    //     console.log(i,' ',sum)
    // }
    // console.log('s',sum)
    if (a % 2 === 0) {
        switch (n % 4) {
            case 1: {
                console.log(-n+a)
                break;
            }
            case 2: {
                console.log(1+a)
                break;
            }
            case 3: {
                console.log(n+1+a)
                break;
            }
            case 0: {
                console.log(a)
                break;
            }
        }
    } else {
        switch (n % 4) {
            case 1: {
                console.log(n+a)
                break;
            }
            case 2: {
                console.log(n-1)
                break;
            }
            case 3: {
                console.log(-n+a-1)
                break;
            }
            case 0: {
                console.log(a)
                break;
            }
        }

    }
}

function main() {
    for (let i = 0; i < inputArr.length; i++) solve(inputArr[i])
}

"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");

function print(x) {
  console.log(x);
}
let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

function main(){
  let input = Number(readline().replace('\r',''));
  let output = [];
  for(let i=0; i<input; i++){
    let point = readline().replace('\r','').split(' ');
    output.push(position(Number(point[0]), Number(point[1])))
  }
  for(let j=0; j<output.length; j++){
    print(output[j])
  }
}

function position(x, jump){
  let m = 1;
  if(x%2===0){
  m = -1;
  }

  let j = jump - 1;

  if(j%4===1) {
  x = x + Math.floor(jump/4)*4*m + m + m*jump*m;
  }

  else if(j%4===2){
  x = x + (jump-1)*(-2)*m + Math.floor(jump/4)*4*m;
  }

  else if(j%4===3) {
  x = x;
  }

  else if(j%4===0){
  x = x + Math.floor(jump/4)*4*m + m;
  }

  return x;
}